Security Advisory: FreeBSD-SA-00:53.catopen

2000-09-28T00:00:00
ID SECURITYVULNS:DOC:720
Type securityvulns
Reporter Securityvulns
Modified 2000-09-28T00:00:00

Description

-----BEGIN PGP SIGNED MESSAGE-----

============================================================================= FreeBSD-SA-00:53 Security Advisory FreeBSD, Inc.

Topic: catopen() may pose security risk for third party code

Category: core Module: libc Announced: 2000-09-27 Affects: FreeBSD 5.0-CURRENT, 4.x and 3.x prior to the correction date. Corrected: Problem 1: 2000-08-06 (FreeBSD 5.0-CURRENT) 2000-08-22 (FreeBSD 4.1-STABLE) 2000-09-07 (FreeBSD 3.5-STABLE) Problem 2: 2000-09-08 (FreeBSD 5.0-CURRENT, 4.1-STABLE and 3.5-STABLE) Credits: Problem 1: Discovered during internal auditing Problem 2: Ivan Arce <iarce@core-sdi.com> FreeBSD only: NO

I. Background

catopen() and setlocale() are functions which are used to display text in a localized format, e.g. for international users.

II. Problem Description

There are two problems addressed in this advisory:

1) The catopen() function did not correctly bounds-check an internal buffer which could be indirectly overflowed by the setting of an environment variable. A privileged application which uses catopen() could be made to execute arbitrary code by an unprivileged local user.

2) The catopen() and setlocale() functions could be made to use an arbitrary file as the source for localized data and message catalogs, instead of one of the system files. An attacker could create a file which is a valid locale file or message catalog but which contains special formatting characters which may allow certain badly written privileged applications to be exploited and execute arbitrary code as the privileged user.

This second vulnerability is slightly different from the problem originally discovered by Ivan Arce of Core-SDI which affects multiple UNIX operating systems, which involved a different environment variable and which FreeBSD is not susceptible to. However Vulnerability 2 was discovered in FreeBSD after the publication the Core-SDI advisory, and has the same effect on vulnerable applications.

NOTE that the FreeBSD base system is not believed to be vulnerable to either of these problems, nor are any vulnerable third party programs (including FreeBSD ports) currently known. Therefore the impact on the majority of FreeBSD systems is expected to be nonexistent.

III. Impact

Certain setuid/setgid third-party software (including FreeBSD ports/packages) may be vulnerable to a local exploit yielding privileged access. No such software is however currently known.

It is believed that no program in the FreeBSD base system is vulnerable to these bugs.

The problems were corrected prior to the release of FreeBSD 4.1.1.

IV. Workaround

Vulnerability 1 described above is the more serious of the two, since it does not require the application to contain a coding flaw in order to exploit it. A scanning utility is provided to detect privileged binaries which use the catopen() function (both statically and dynamically linked binaries), which should be either rebuilt, or have their privileges limited to minimize potential risk.

It is not feasible to detect binaries which are vulnerable to the second vulnerability, however the provided utility will also report statically linked binaries which use the setlocale() functions and which may potentially be vulnerable. Most of the binaries reported will not in fact be vulnerable, but should be recompiled anyway for maximum assurance of security. Note that some FreeBSD system binaries may be reported as possibly vulnerable by this script, however this is not the case.

Statically linked binaries which are identified as vulnerable or potentially vulnerable should be recompiled from source code after patching and recompiling libc, if possible, in order to correct the vulnerability. Dynamically linked binaries will be corrected by simply patching and recompiling libc as described below.

As an interim measure, consider removing any identified setuid or setgid binary, removing set[ug]id privileges from the file, or limiting the file access permissions, as appropriate.

Of course, it is possible that some of the identified files may be required for the correct operation of your local system, in which case there is no clear workaround except for limiting the set of users who may run the binaries, by an appropriate use of user groups and removing the "o+x" file permission bit.

1) Download the 'scan_locale.sh' and 'test_locale.sh' scripts from

ftp://ftp.freebsd.org/pub/FreeBSD/CERT/tools/SA-00:53/scan_locale.sh ftp://ftp.freebsd.org/pub/FreeBSD/CERT/tools/SA-00:53/test_locale.sh

e.g. with the fetch(1) command:

fetch ftp://ftp.freebsd.org/pub/FreeBSD/CERT/tools/SA-00:53/scan_locale.sh

Receiving scan_locale.sh (337 bytes): 100% 337 bytes transferred in 0.0 seconds (1.05 MBps)

fetch ftp://ftp.freebsd.org/pub/FreeBSD/CERT/tools/SA-00:53/test_locale.sh

Receiving test_locale.sh (889 bytes): 100% 889 bytes transferred in 0.0 seconds (1.34 MBps)

2) Verify the md5 checksums and compare to the value below:

/sbin/md5 scan_locale.sh

MD5 (scan_locale.sh) = efea80f74b05e7ddbc0261ef5211e453

/sbin/md5 test_locale.sh

MD5 (test_locale.sh) = 2a485bf8171cc984dbc58b4d545668b4

3) Run the scan_locale.sh script against your system:

sh scan_locale.sh ./test_locale.sh /

This will scan your entire system for setuid or setgid binaries which make use of the exploitable function catopen(), or the potentially exploitable function setlocale(). Each returned binary should be examined (e.g. with 'ls -l' and/or other tools) to determine what security risk it poses to your local environment, e.g. whether it can be run by arbitrary local users who may be able to exploit it to gain privileges.

Note that this script reports setlocale() usage (i.e. vulnerability 2) only in statically linked binaries, not dynamically linked binaries, because of the high rate of false positives. It is likely that the majority of such setlocale() binaries identified are not insecure and their identification by this script should not be taken as evidence that they are vulnerable, but they should be recompiled anyway for maximum assurance of security.

4) Remove the binaries, or reduce their file permissions, as appropriate.

V. Solution

Upgrade your vulnerable FreeBSD system to 4.1-STABLE or 3.5-STABLE after the correction date, or patch your present system source code and rebuild. Then run the scan_locale.sh script as instructed in section IV and identify any statically-linked binaries as reported by the script. These should either be removed, recompiled, or have privileges restricted to secure them against this vulnerability (since statically-linked binaries will not be affected by simply recompiling the shared libc library).

To patch your present system: save the patch below into a file, and execute the following commands as root:

cd /usr/src/lib/libc patch < /path/to/patch/file make all make install

Patches for FreeBSD systems before the correction date:

Index: msgcat.c
===================================================================
RCS file: /usr2/ncvs//src/lib/libc/nls/msgcat.c,v
retrieving revision 1.21
retrieving revision 1.27
diff -u -r1.21 -r1.27
--- nls/msgcat.c    2000/01/27 23:06:33     1.21
+++ nls/msgcat.c    2000/09/01 11:56:31     1.27
@@ -91,8 +91,9 @@
     __const char *catpath = NULL;
     char        *nlspath;
     char   *lang;
-    long   len;
     char   *base, *cptr, *pathP;
+    int            spcleft;
+    long   len;
     struct stat    sbuf;

     if &#40;!name || !*name&#41; {
@@ -106,10 +107,10 @@
     } else {
    if &#40;type == NL_CAT_LOCALE&#41;
            lang = setlocale&#40;LC_MESSAGES, NULL&#41;;
-   else {
-           if &#40;&#40;lang = &#40;char *&#41; getenv&#40;&quot;LANG&quot;&#41;&#41; == NULL&#41;
-                   lang = &quot;C&quot;;
-   }
+   else
+           lang = getenv&#40;&quot;LANG&quot;&#41;;
+   if &#40;lang == NULL || strchr&#40;lang, &#39;/&#39;&#41; != NULL&#41;
+           lang = &quot;C&quot;;
    if &#40;&#40;nlspath = &#40;char *&#41; getenv&#40;&quot;NLSPATH&quot;&#41;&#41; == NULL
 #ifndef __NETBSD_SYSCALLS
        || issetugid&#40;&#41;
@@ -129,13 +130,22 @@
            *cptr = &#39;&#92;0&#39;;
            for &#40;pathP = path; *nlspath; ++nlspath&#41; {
                if &#40;*nlspath == &#39;&#37;&#39;&#41; {
+                   spcleft = sizeof&#40;path&#41; - &#40;pathP - path&#41;;
                    if &#40;*&#40;nlspath + 1&#41; == &#39;L&#39;&#41; {
                        ++nlspath;
-                       strcpy&#40;pathP, lang&#41;;
+                       if &#40;strlcpy&#40;pathP, lang, spcleft&#41; &gt;= spcleft&#41; {
+                           free&#40;base&#41;;
+                           errno = ENAMETOOLONG;
+                           return&#40;NLERR&#41;;
+                       }
                        pathP += strlen&#40;lang&#41;;
                    } else if &#40;*&#40;nlspath + 1&#41; == &#39;N&#39;&#41; {
                        ++nlspath;
-                       strcpy&#40;pathP, name&#41;;
+                       if &#40;strlcpy&#40;pathP, name, spcleft&#41; &gt;= spcleft&#41; {
+                           free&#40;base&#41;;
+                           errno = ENAMETOOLONG;
+                           return&#40;NLERR&#41;;
+                       }
                        pathP += strlen&#40;name&#41;;
                    } else *&#40;pathP++&#41; = *nlspath;
                } else *&#40;pathP++&#41; = *nlspath;
@@ -186,7 +196,7 @@
     MCSetT *set;
     long   lo, hi, cur, dir;

-    if &#40;!cat || setId &lt;= 0&#41; return&#40;NULL&#41;;
+    if &#40;cat == NULL || setId &lt;= 0&#41; return&#40;NULL&#41;;

     lo = 0;
     if &#40;setId - 1 &lt; cat-&gt;numSets&#41; {
@@ -212,8 +222,8 @@
    if &#40;hi - lo == 1&#41; cur += dir;
    else cur += &#40;&#40;hi - lo&#41; / 2&#41; * dir;
     }
-    if &#40;set-&gt;invalid&#41;
-   &#40;void&#41; loadSet&#40;cat, set&#41;;
+    if &#40;set-&gt;invalid &amp;&amp; loadSet&#40;cat, set&#41; &lt;= 0&#41;
+   return&#40;NULL&#41;;
     return&#40;set&#41;;
 }

@@ -225,7 +235,7 @@
     MCMsgT *msg;
     long   lo, hi, cur, dir;

-    if &#40;!set || set-&gt;invalid || msgId &lt;= 0&#41; return&#40;NULL&#41;;
+    if &#40;set == NULL || set-&gt;invalid || msgId &lt;= 0&#41; return&#40;NULL&#41;;

     lo = 0;
     if &#40;msgId - 1 &lt; set-&gt;numMsgs&#41; {
@@ -318,7 +328,7 @@
     off_t  nextSet;

     cat = &#40;MCCatT *&#41; malloc&#40;sizeof&#40;MCCatT&#41;&#41;;
-    if &#40;!cat&#41; return&#40;NLERR&#41;;
+    if &#40;cat == NULL&#41; return&#40;NLERR&#41;;
     cat-&gt;loadType = MCLoadBySet;

     if &#40;&#40;cat-&gt;fd = _open&#40;catpath, O_RDONLY&#41;&#41; &lt; 0&#41; {
@@ -351,7 +361,7 @@

     cat-&gt;numSets = header.numSets;
     cat-&gt;sets = &#40;MCSetT *&#41; malloc&#40;sizeof&#40;MCSetT&#41; * header.numSets&#41;;
-    if &#40;!cat-&gt;sets&#41; NOSPACE&#40;&#41;;
+    if &#40;cat-&gt;sets == NULL&#41; NOSPACE&#40;&#41;;

     nextSet = header.firstSet;
     for &#40;i = 0; i &lt; cat-&gt;numSets; ++i&#41; {
Index: setlocale.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/locale/setlocale.c,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -r1.27 -r1.28
--- locale/setlocale.c      2000/09/04 03:43:24     1.27
+++ locale/setlocale.c      2000/09/08 07:29:48     1.28
@@ -129,7 +129,7 @@
            if &#40;!env || !*env&#41;
                    env = getenv&#40;&quot;LANG&quot;&#41;;

-           if &#40;!env || !*env&#41;
+           if &#40;!env || !*env || strchr&#40;env, &#39;/&#39;&#41;&#41;
                    env = &quot;C&quot;;

            &#40;void&#41; strncpy&#40;new_categories[category], env, ENCODING_LEN&#41;;

-----BEGIN PGP SIGNATURE----- Version: 2.6.2

iQCVAwUBOdKTo1UuHi5z0oilAQH9QwQAhEdiXOU7A/hZpMBKU5bWz6alLqr7o4wp YcypPTnSoMQ2OkFlmuX9sdcgRfwl3gZ1z3QfjhE/eXG7rYSerEyxqcBqgQOBbCUH vURxPEIRqV90DMMZAp62viA1X1Vyx/Ie2WXG/r5Wck1/Zu6BSxsUo3yiWD4gFoVb L1f0kBgl2/A= =YtCH -----END PGP SIGNATURE-----

To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-security" in the body of the message