Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 102520 - sys-apps/file-4.14 reads beyond end of buffer
Summary: sys-apps/file-4.14 reads beyond end of buffer
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Security
Classification: Unclassified
Component: Auditing (show other bugs)
Hardware: All Linux
: High normal (vote)
Assignee: Gentoo's Team for Core System packages
URL:
Whiteboard:
Keywords:
: 102504 (view as bug list)
Depends on:
Blocks:
 
Reported: 2005-08-14 10:58 UTC by Daniel Drake (RETIRED)
Modified: 2005-08-18 15:27 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments
Fix (file.patch,450 bytes, patch)
2005-08-14 11:00 UTC, Daniel Drake (RETIRED)
Details | Diff
Sample file (badfile,250.00 KB, patch)
2005-08-14 11:04 UTC, Daniel Drake (RETIRED)
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Drake (RETIRED) gentoo-dev 2005-08-14 10:58:39 UTC
I've found and identified a bug in file which causes it to read beyond the end
of a buffer. Not sure if this is a security issue (as its only a 'read
overflow') but I thought I should check with you first.

mget() in softmagic.c will perform a memcpy operation and in some circumstances
will attempt to read beyond the end of the source buffer.

It does do *some* bounds checking but in the case of FILE_SEARCH this is wrong.
(approx line 1125)
		case FILE_STRING:
		case FILE_PSTRING:
		case FILE_SEARCH:
			if (nbytes < (offset + m->vallen))
				return 0;

(approx line 1135)
	if (m->type == FILE_SEARCH) {
		p->buf = malloc(m->mask + m->vallen);
		if (p->buf == NULL) {
			file_error(ms, errno, "Cannot allocate search buffer");
			return 0;
		}
		(void)memcpy(p->buf, s + offset, m->mask + m->vallen);
	}

You can see it will read mask+vallen bytes starting at s+offset. However there
is no check that s+offset+mask+vallen (i.e. the end of the data being copied) is
still within the bounds of s.

'nbytes' refers to the size of s.
Comment 1 Daniel Drake (RETIRED) gentoo-dev 2005-08-14 11:00:07 UTC
Created attachment 65949 [details, diff]
Fix
Comment 2 Daniel Drake (RETIRED) gentoo-dev 2005-08-14 11:04:31 UTC
Created attachment 65950 [details, diff]
Sample file

This file will cause the read overflow.

An unpatched file-4.14 tries to read 12292 bytes starting at offset 255488 from
s. s is just an in-memory copy of this file, 256000 bytes, so it obviously goes
much too far.

Even without the patch, file still happily works on this on my x86. However on
amd64 it seems much easier to reproduce - you get a Segmentation fault.
Comment 3 Daniel Drake (RETIRED) gentoo-dev 2005-08-14 11:05:17 UTC
*** Bug 102504 has been marked as a duplicate of this bug. ***
Comment 4 Tavis Ormandy (RETIRED) gentoo-dev 2005-08-14 13:26:40 UTC
Does not appear to be any security impact, reassigning to base-system.
Comment 5 Daniel Drake (RETIRED) gentoo-dev 2005-08-15 10:33:02 UTC
Sent patch upstream
Comment 6 Daniel Drake (RETIRED) gentoo-dev 2005-08-15 14:35:14 UTC
Upstream maintainer confirms the problem and says it exists in other places too.
Expect a 4.15 release soon.
Comment 7 SpanKY gentoo-dev 2005-08-18 15:27:16 UTC
file-4.15 now in portage