<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "http://bugs.gentoo.org/bugzilla.dtd">

<bugzilla version="2.22.7"
          urlbase="http://bugs.gentoo.org/"
          maintainer="bugzilla@gentoo.org"
>

    <bug>
          <bug_id>207926</bug_id>
          
          <creation_ts>2008-01-28 16:51 0000</creation_ts>
          <short_desc>sys-devel/gcc-config has buffer overflows</short_desc>
          <delta_ts>2008-03-16 01:20:25 0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>Gentoo Linux</product>
          <component>Core system</component>
          <version>unspecified</version>
          <rep_platform>All</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          
          <priority>P2</priority>
          <bug_severity>minor</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          
          <everconfirmed>1</everconfirmed>
          <reporter>eteran@alum.rit.edu</reporter>
          <assigned_to>toolchain@gentoo.org</assigned_to>
          <cc>tcunha@gentoo.org</cc>

      

      
          <long_desc isprivate="0">
            <who>eteran@alum.rit.edu</who>
            <bug_when>2008-01-28 16:51:33 0000</bug_when>
            <thetext>I was looking into why gentoo uses gcc-config instead of an &quot;eselect-gcc&quot; which would be more consistant with the rest of gentoo. So I figured that I would look at the gcc-config sources, after all, it essentially does similar work to eselect-gcc, perhaps I could help :)

Anyway, I discovered misuse of several functions causing buffer overflows. My examples are from wrapper-1.5.0.c:

line 328:
    &quot;strcpy(data.name, basename(argv[0]));&quot; more hypothetical than practical, but since you don&apos;t know the length of argv[0], it should be a strncpy.

lines 218-221:
        &quot;strncpy(data-&gt;bin, str, sizeof(data-&gt;bin) - 1);
	data-&gt;bin[strlen(data-&gt;bin) - 1] = &apos;/&apos;;
	strncat(data-&gt;bin, data-&gt;name, sizeof(data-&gt;bin) - 1);
	data-&gt;bin[MAXPATHLEN] = 0;&quot;

here, the problem is misuse of the strncat function. The size parameter is supposed to represent how much is LEFT in the buffer (total size - used part). Not the total size. I will attach a program which demonstrates that it does in fact cause an overflow. Fortunately, the only damage that is done is the data-&gt;tmp variable gets trashed. However, this whole thing could be safely replaced with something like this:

snprintf(data-&gt;bin, sizeof(data-&gt;bin), &quot;%s/%s&quot;, str, data-&gt;name);

line 48:
     strncpy does not gaurantee null termination for long strings

line 71-73:
    &quot;size_t len = strlen(path) + strlen(data-&gt;name) + 2;
     snprintf(str, len, &quot;%s/%s&quot;, path, data-&gt;name);&quot;

The size param of snprintf should be the size of the buffer, not the size of the source data! This code basically does no bounds checking at all, because you set the len equal to the size of the source data.

These should be easy to clean up, but of course I am still interested in my end gaol of making gcc-config more consistent with the eselect system. But that is for another day :)

Thanks,
Evan Teran

Reproducible: Always</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>eteran@alum.rit.edu</who>
            <bug_when>2008-01-28 16:52:21 0000</bug_when>
            <thetext>Created an attachment (id=142028)
example of overflow

</thetext>
          </long_desc>
          <long_desc isprivate="0">
            <who>vapier@gentoo.org</who>
            <bug_when>2008-03-16 01:20:25 0000</bug_when>
            <thetext>eselect-gcc is a duplicate of gcc-config at the moment, not the other way around

string handling has been cleaned up in gcc-config-1.4.1:
http://sources.gentoo.org/sys-devel/gcc-config/files/wrapper-1.5.1.c?rev=1.1</thetext>
          </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>142028</attachid>
            <date>2008-01-28 16:52 0000</date>
            <desc>example of overflow</desc>
            <filename>test.c</filename>
            <type>text/plain</type>
            <data encoding="base64">CiNkZWZpbmUgX1JFRU5UUkFOVAojZGVmaW5lIF9HTlVfU09VUkNFCgojZGVmaW5lIE1BWFBBVEhM
RU4gNDA5NgoKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUg
PHN5cy90eXBlcy5oPgojaW5jbHVkZSA8c3lz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