olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1133812-2.patch

00db10
commit 585367266923156ac6fb789939a923641ba5aaf4
00db10
Author: Florian Weimer <fweimer@redhat.com>
00db10
Date:   Wed May 28 14:05:03 2014 +0200
00db10
00db10
    manual: Update the locale documentation
00db10
00db10
commit 4e8f95a0df7c2300b830ec12c0ae1e161bc8a8a3
00db10
Author: Florian Weimer <fweimer@redhat.com>
00db10
Date:   Mon May 12 15:24:12 2014 +0200
00db10
00db10
    _nl_find_locale: Improve handling of crafted locale names [BZ #17137]
00db10
    
00db10
    Prevent directory traversal in locale-related environment variables
00db10
    (CVE-2014-0475).
00db10
00db10
commit d183645616b0533b3acee28f1a95570bffbdf50f
00db10
Author: Florian Weimer <fweimer@redhat.com>
00db10
Date:   Wed May 28 14:41:52 2014 +0200
00db10
00db10
    setlocale: Use the heap for the copy of the locale argument
00db10
    
00db10
    This avoids alloca calls with potentially large arguments.
00db10
00db10
diff -pruN glibc-2.17-c758a686/locale/findlocale.c glibc-2.17-c758a686/locale/findlocale.c
00db10
--- glibc-2.17-c758a686/locale/findlocale.c	2013-08-11 04:22:55.000000000 +0530
00db10
+++ glibc-2.17-c758a686/locale/findlocale.c	2014-08-26 16:14:50.403253778 +0530
00db10
@@ -17,6 +17,7 @@
00db10
    <http://www.gnu.org/licenses/>.  */
00db10
 
00db10
 #include <assert.h>
00db10
+#include <errno.h>
00db10
 #include <locale.h>
00db10
 #include <stdlib.h>
00db10
 #include <string.h>
00db10
@@ -57,6 +58,45 @@ struct loaded_l10nfile *_nl_locale_file_
00db10
 
00db10
 const char _nl_default_locale_path[] attribute_hidden = LOCALEDIR;
00db10
 
00db10
+/* Checks if the name is actually present, that is, not NULL and not
00db10
+   empty.  */
00db10
+static inline int
00db10
+name_present (const char *name)
00db10
+{
00db10
+  return name != NULL && name[0] != '\0';
00db10
+}
00db10
+
00db10
+/* Checks that the locale name neither extremely long, nor contains a
00db10
+   ".." path component (to prevent directory traversal).  */
00db10
+static inline int
00db10
+valid_locale_name (const char *name)
00db10
+{
00db10
+  /* Not set.  */
00db10
+  size_t namelen = strlen (name);
00db10
+  /* Name too long.  The limit is arbitrary and prevents stack overflow
00db10
+     issues later.  */
00db10
+  if (__glibc_unlikely (namelen > 255))
00db10
+    return 0;
00db10
+  /* Directory traversal attempt.  */
00db10
+  static const char slashdot[4] = {'/', '.', '.', '/'};
00db10
+  if (__glibc_unlikely (memmem (name, namelen,
00db10
+				slashdot, sizeof (slashdot)) != NULL))
00db10
+    return 0;
00db10
+  if (namelen == 2 && __glibc_unlikely (name[0] == '.' && name [1] == '.'))
00db10
+    return 0;
00db10
+  if (namelen >= 3
00db10
+      && __glibc_unlikely (((name[0] == '.'
00db10
+			     && name[1] == '.'
00db10
+			     && name[2] == '/')
00db10
+			    || (name[namelen - 3] == '/'
00db10
+				&& name[namelen - 2] == '.'
00db10
+				&& name[namelen - 1] == '.'))))
00db10
+    return 0;
00db10
+  /* If there is a slash in the name, it must start with one.  */
00db10
+  if (__glibc_unlikely (memchr (name, '/', namelen) != NULL) && name[0] != '/')
00db10
+    return 0;
00db10
+  return 1;
00db10
+}
00db10
 
00db10
 struct __locale_data *
00db10
 internal_function
00db10
@@ -65,7 +105,7 @@ _nl_find_locale (const char *locale_path
00db10
 {
00db10
   int mask;
00db10
   /* Name of the locale for this category.  */
00db10
-  char *loc_name;
00db10
+  char *loc_name = (char *) *name;
00db10
   const char *language;
00db10
   const char *modifier;
00db10
   const char *territory;
00db10
@@ -73,31 +113,39 @@ _nl_find_locale (const char *locale_path
00db10
   const char *normalized_codeset;
00db10
   struct loaded_l10nfile *locale_file;
00db10
 
00db10
-  if ((*name)[0] == '\0')
00db10
+  if (loc_name[0] == '\0')
00db10
     {
00db10
       /* The user decides which locale to use by setting environment
00db10
 	 variables.  */
00db10
-      *name = getenv ("LC_ALL");
00db10
-      if (*name == NULL || (*name)[0] == '\0')
00db10
-	*name = getenv (_nl_category_names.str
00db10
+      loc_name = getenv ("LC_ALL");
00db10
+      if (!name_present (loc_name))
00db10
+	loc_name = getenv (_nl_category_names.str
00db10
 			+ _nl_category_name_idxs[category]);
00db10
-      if (*name == NULL || (*name)[0] == '\0')
00db10
-	*name = getenv ("LANG");
00db10
+      if (!name_present (loc_name))
00db10
+	loc_name = getenv ("LANG");
00db10
+      if (!name_present (loc_name))
00db10
+	loc_name = (char *) _nl_C_name;
00db10
     }
00db10
 
00db10
-  if (*name == NULL || (*name)[0] == '\0'
00db10
-      || (__builtin_expect (__libc_enable_secure, 0)
00db10
-	  && strchr (*name, '/') != NULL))
00db10
-    *name = (char *) _nl_C_name;
00db10
+  /* We used to fall back to the C locale if the name contains a slash
00db10
+     character '/', but we now check for directory traversal in
00db10
+     valid_locale_name, so this is no longer necessary.  */
00db10
 
00db10
-  if (__builtin_expect (strcmp (*name, _nl_C_name), 1) == 0
00db10
-      || __builtin_expect (strcmp (*name, _nl_POSIX_name), 1) == 0)
00db10
+  if (__builtin_expect (strcmp (loc_name, _nl_C_name), 1) == 0
00db10
+      || __builtin_expect (strcmp (loc_name, _nl_POSIX_name), 1) == 0)
00db10
     {
00db10
       /* We need not load anything.  The needed data is contained in
00db10
 	 the library itself.  */
00db10
       *name = (char *) _nl_C_name;
00db10
       return _nl_C[category];
00db10
     }
00db10
+  else if (!valid_locale_name (loc_name))
00db10
+    {
00db10
+      __set_errno (EINVAL);
00db10
+      return NULL;
00db10
+    }
00db10
+
00db10
+  *name = loc_name;
00db10
 
00db10
   /* We really have to load some data.  First we try the archive,
00db10
      but only if there was no LOCPATH environment variable specified.  */
00db10
diff -pruN glibc-2.17-c758a686/locale/setlocale.c glibc-2.17-c758a686/locale/setlocale.c
00db10
--- glibc-2.17-c758a686/locale/setlocale.c	2013-08-11 04:22:55.000000000 +0530
00db10
+++ glibc-2.17-c758a686/locale/setlocale.c	2014-08-26 16:14:50.401253764 +0530
00db10
@@ -272,6 +272,8 @@ setlocale (int category, const char *loc
00db10
 	 of entries of the form `CATEGORY=VALUE'.  */
00db10
       const char *newnames[__LC_LAST];
00db10
       struct __locale_data *newdata[__LC_LAST];
00db10
+      /* Copy of the locale argument, for in-place splitting.  */
00db10
+      char *locale_copy = NULL;
00db10
 
00db10
       /* Set all name pointers to the argument name.  */
00db10
       for (category = 0; category < __LC_LAST; ++category)
00db10
@@ -281,7 +283,13 @@ setlocale (int category, const char *loc
00db10
       if (__builtin_expect (strchr (locale, ';') != NULL, 0))
00db10
 	{
00db10
 	  /* This is a composite name.  Make a copy and split it up.  */
00db10
-	  char *np = strdupa (locale);
00db10
+	  locale_copy = strdup (locale);
00db10
+	  if (__glibc_unlikely (locale_copy == NULL))
00db10
+	    {
00db10
+	      __libc_rwlock_unlock (__libc_setlocale_lock);
00db10
+	      return NULL;
00db10
+	    }
00db10
+	  char *np = locale_copy;
00db10
 	  char *cp;
00db10
 	  int cnt;
00db10
 
00db10
@@ -299,6 +307,7 @@ setlocale (int category, const char *loc
00db10
 		{
00db10
 		error_return:
00db10
 		  __libc_rwlock_unlock (__libc_setlocale_lock);
00db10
+		  free (locale_copy);
00db10
 
00db10
 		  /* Bogus category name.  */
00db10
 		  ERROR_RETURN;
00db10
@@ -391,8 +400,9 @@ setlocale (int category, const char *loc
00db10
       /* Critical section left.  */
00db10
       __libc_rwlock_unlock (__libc_setlocale_lock);
00db10
 
00db10
-      /* Free the resources (the locale path variable).  */
00db10
+      /* Free the resources.  */
00db10
       free (locale_path);
00db10
+      free (locale_copy);
00db10
 
00db10
       return composite;
00db10
     }
00db10
diff -pruN glibc-2.17-c758a686/localedata/Makefile glibc-2.17-c758a686/localedata/Makefile
00db10
--- glibc-2.17-c758a686/localedata/Makefile	2014-08-26 16:15:22.656474571 +0530
00db10
+++ glibc-2.17-c758a686/localedata/Makefile	2014-08-26 16:14:50.403253778 +0530
00db10
@@ -77,7 +77,7 @@ locale_test_suite := tst_iswalnum tst_is
00db10
 
00db10
 tests = $(locale_test_suite) tst-digits tst-setlocale bug-iconv-trans \
00db10
 	tst-leaks tst-mbswcs6 tst-xlocale1 tst-xlocale2 bug-usesetlocale \
00db10
-	tst-strfmon1 tst-sscanf bug-setlocale1 tst-setlocale2
00db10
+	tst-strfmon1 tst-sscanf bug-setlocale1 tst-setlocale2 tst-setlocale3
00db10
 ifeq (yes,$(build-shared))
00db10
 ifneq (no,$(PERL))
00db10
 tests: $(objpfx)mtrace-tst-leaks
00db10
diff -pruN glibc-2.17-c758a686/localedata/tst-setlocale3.c glibc-2.17-c758a686/localedata/tst-setlocale3.c
00db10
--- glibc-2.17-c758a686/localedata/tst-setlocale3.c	1970-01-01 05:30:00.000000000 +0530
00db10
+++ glibc-2.17-c758a686/localedata/tst-setlocale3.c	2014-08-26 16:14:50.403253778 +0530
00db10
@@ -0,0 +1,203 @@
00db10
+/* Regression test for setlocale invalid environment variable handling.
00db10
+   Copyright (C) 2014 Free Software Foundation, Inc.
00db10
+   This file is part of the GNU C Library.
00db10
+
00db10
+   The GNU C Library is free software; you can redistribute it and/or
00db10
+   modify it under the terms of the GNU Lesser General Public
00db10
+   License as published by the Free Software Foundation; either
00db10
+   version 2.1 of the License, or (at your option) any later version.
00db10
+
00db10
+   The GNU C Library is distributed in the hope that it will be useful,
00db10
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
00db10
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00db10
+   Lesser General Public License for more details.
00db10
+
00db10
+   You should have received a copy of the GNU Lesser General Public
00db10
+   License along with the GNU C Library; if not, see
00db10
+   <http://www.gnu.org/licenses/>.  */
00db10
+
00db10
+#include <locale.h>
00db10
+#include <stdio.h>
00db10
+#include <stdlib.h>
00db10
+#include <string.h>
00db10
+
00db10
+/* The result of setlocale may be overwritten by subsequent calls, so
00db10
+   this wrapper makes a copy.  */
00db10
+static char *
00db10
+setlocale_copy (int category, const char *locale)
00db10
+{
00db10
+  const char *result = setlocale (category, locale);
00db10
+  if (result == NULL)
00db10
+    return NULL;
00db10
+  return strdup (result);
00db10
+}
00db10
+
00db10
+static char *de_locale;
00db10
+
00db10
+static void
00db10
+setlocale_fail (const char *envstring)
00db10
+{
00db10
+  setenv ("LC_CTYPE", envstring, 1);
00db10
+  if (setlocale (LC_CTYPE, "") != NULL)
00db10
+    {
00db10
+      printf ("unexpected setlocale success for \"%s\" locale\n", envstring);
00db10
+      exit (1);
00db10
+    }
00db10
+  const char *newloc = setlocale (LC_CTYPE, NULL);
00db10
+  if (strcmp (newloc, de_locale) != 0)
00db10
+    {
00db10
+      printf ("failed setlocale call \"%s\" changed locale to \"%s\"\n",
00db10
+	      envstring, newloc);
00db10
+      exit (1);
00db10
+    }
00db10
+}
00db10
+
00db10
+static void
00db10
+setlocale_success (const char *envstring)
00db10
+{
00db10
+  setenv ("LC_CTYPE", envstring, 1);
00db10
+  char *newloc = setlocale_copy (LC_CTYPE, "");
00db10
+  if (newloc == NULL)
00db10
+    {
00db10
+      printf ("setlocale for \"%s\": %m\n", envstring);
00db10
+      exit (1);
00db10
+    }
00db10
+  if (strcmp (newloc, de_locale) == 0)
00db10
+    {
00db10
+      printf ("setlocale with LC_CTYPE=\"%s\" left locale at \"%s\"\n",
00db10
+	      envstring, de_locale);
00db10
+      exit (1);
00db10
+    }
00db10
+  if (setlocale (LC_CTYPE, de_locale) == NULL)
00db10
+    {
00db10
+      printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
00db10
+	      de_locale, envstring);
00db10
+      exit (1);
00db10
+    }
00db10
+  char *newloc2 = setlocale_copy (LC_CTYPE, newloc);
00db10
+  if (newloc2 == NULL)
00db10
+    {
00db10
+      printf ("restoring locale \"%s\" following \"%s\": %m\n",
00db10
+	      newloc, envstring);
00db10
+      exit (1);
00db10
+    }
00db10
+  if (strcmp (newloc, newloc2) != 0)
00db10
+    {
00db10
+      printf ("representation of locale \"%s\" changed from \"%s\" to \"%s\"",
00db10
+	      envstring, newloc, newloc2);
00db10
+      exit (1);
00db10
+    }
00db10
+  free (newloc);
00db10
+  free (newloc2);
00db10
+
00db10
+  if (setlocale (LC_CTYPE, de_locale) == NULL)
00db10
+    {
00db10
+      printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
00db10
+	      de_locale, envstring);
00db10
+      exit (1);
00db10
+    }
00db10
+}
00db10
+
00db10
+/* Checks that a known-good locale still works if LC_ALL contains a
00db10
+   value which should be ignored.  */
00db10
+static void
00db10
+setlocale_ignore (const char *to_ignore)
00db10
+{
00db10
+  const char *fr_locale = "fr_FR.UTF-8";
00db10
+  setenv ("LC_CTYPE", fr_locale, 1);
00db10
+  char *expected_locale = setlocale_copy (LC_CTYPE, "");
00db10
+  if (expected_locale == NULL)
00db10
+    {
00db10
+      printf ("setlocale with LC_CTYPE=\"%s\" failed: %m\n", fr_locale);
00db10
+      exit (1);
00db10
+    }
00db10
+  if (setlocale (LC_CTYPE, de_locale) == NULL)
00db10
+    {
00db10
+      printf ("failed to restore locale: %m\n");
00db10
+      exit (1);
00db10
+    }
00db10
+  unsetenv ("LC_CTYPE");
00db10
+
00db10
+  setenv ("LC_ALL", to_ignore, 1);
00db10
+  setenv ("LC_CTYPE", fr_locale, 1);
00db10
+  const char *actual_locale = setlocale (LC_CTYPE, "");
00db10
+  if (actual_locale == NULL)
00db10
+    {
00db10
+      printf ("setlocale with LC_ALL, LC_CTYPE=\"%s\" failed: %m\n",
00db10
+	      fr_locale);
00db10
+      exit (1);
00db10
+    }
00db10
+  if (strcmp (actual_locale, expected_locale) != 0)
00db10
+    {
00db10
+      printf ("setlocale under LC_ALL failed: got \"%s\", expected \"%s\"\n",
00db10
+	      actual_locale, expected_locale);
00db10
+      exit (1);
00db10
+    }
00db10
+  unsetenv ("LC_CTYPE");
00db10
+  setlocale_success (fr_locale);
00db10
+  unsetenv ("LC_ALL");
00db10
+  free (expected_locale);
00db10
+}
00db10
+
00db10
+static int
00db10
+do_test (void)
00db10
+{
00db10
+  /* The glibc test harness sets this environment variable
00db10
+     uncondionally.  */
00db10
+  unsetenv ("LC_ALL");
00db10
+
00db10
+  de_locale = setlocale_copy (LC_CTYPE, "de_DE.UTF-8");
00db10
+  if (de_locale == NULL)
00db10
+    {
00db10
+      printf ("setlocale (LC_CTYPE, \"de_DE.UTF-8\"): %m\n");
00db10
+      return 1;
00db10
+    }
00db10
+  setlocale_success ("C");
00db10
+  setlocale_success ("en_US.UTF-8");
00db10
+  setlocale_success ("/en_US.UTF-8");
00db10
+  setlocale_success ("//en_US.UTF-8");
00db10
+  setlocale_ignore ("");
00db10
+
00db10
+  setlocale_fail ("does-not-exist");
00db10
+  setlocale_fail ("/");
00db10
+  setlocale_fail ("/../localedata/en_US.UTF-8");
00db10
+  setlocale_fail ("en_US.UTF-8/");
00db10
+  setlocale_fail ("en_US.UTF-8/..");
00db10
+  setlocale_fail ("en_US.UTF-8/../en_US.UTF-8");
00db10
+  setlocale_fail ("../localedata/en_US.UTF-8");
00db10
+  {
00db10
+    size_t large_length = 1024;
00db10
+    char *large_name = malloc (large_length + 1);
00db10
+    if (large_name == NULL)
00db10
+      {
00db10
+	puts ("malloc failure");
00db10
+	return 1;
00db10
+      }
00db10
+    memset (large_name, '/', large_length);
00db10
+    const char *suffix = "en_US.UTF-8";
00db10
+    strcpy (large_name + large_length - strlen (suffix), suffix);
00db10
+    setlocale_fail (large_name);
00db10
+    free (large_name);
00db10
+  }
00db10
+  {
00db10
+    size_t huge_length = 64 * 1024 * 1024;
00db10
+    char *huge_name = malloc (huge_length + 1);
00db10
+    if (huge_name == NULL)
00db10
+      {
00db10
+	puts ("malloc failure");
00db10
+	return 1;
00db10
+      }
00db10
+    memset (huge_name, 'X', huge_length);
00db10
+    huge_name[huge_length] = '\0';
00db10
+    /* Construct a composite locale specification. */
00db10
+    const char *prefix = "LC_CTYPE=de_DE.UTF-8;LC_TIME=";
00db10
+    memcpy (huge_name, prefix, strlen (prefix));
00db10
+    setlocale_fail (huge_name);
00db10
+    free (huge_name);
00db10
+  }
00db10
+
00db10
+  return 0;
00db10
+}
00db10
+
00db10
+#define TEST_FUNCTION do_test ()
00db10
+#include "../test-skeleton.c"
00db10
diff -pruN glibc-2.17-c758a686/manual/locale.texi glibc-2.17-c758a686/manual/locale.texi
00db10
--- glibc-2.17-c758a686/manual/locale.texi	2013-08-11 04:22:55.000000000 +0530
00db10
+++ glibc-2.17-c758a686/manual/locale.texi	2014-08-26 16:14:50.404253785 +0530
00db10
@@ -29,6 +29,7 @@ will follow the conventions preferred by
00db10
 * Setting the Locale::          How a program specifies the locale
00db10
                                  with library functions.
00db10
 * Standard Locales::            Locale names available on all systems.
00db10
+* Locale Names::                Format of system-specific locale names.
00db10
 * Locale Information::          How to access the information for the locale.
00db10
 * Formatting Numbers::          A dedicated function to format numbers.
00db10
 * Yes-or-No Questions::         Check a Response against the locale.
00db10
@@ -99,14 +100,16 @@ locale named @samp{espana-castellano} to
00db10
 most of Spain.
00db10
 
00db10
 The set of locales supported depends on the operating system you are
00db10
-using, and so do their names.  We can't make any promises about what
00db10
-locales will exist, except for one standard locale called @samp{C} or
00db10
-@samp{POSIX}.  Later we will describe how to construct locales.
00db10
-@comment (@pxref{Building Locale Files}).
00db10
+using, and so do their names, except that the standard locale called
00db10
+@samp{C} or @samp{POSIX} always exist.  @xref{Locale Names}.
00db10
+
00db10
+In order to force the system to always use the default locale, the
00db10
+user can set the @code{LC_ALL} environment variable to @samp{C}.
00db10
 
00db10
 @cindex combining locales
00db10
-A user also has the option of specifying different locales for different
00db10
-purposes---in effect, choosing a mixture of multiple locales.
00db10
+A user also has the option of specifying different locales for
00db10
+different purposes---in effect, choosing a mixture of multiple
00db10
+locales.  @xref{Locale Categories}.
00db10
 
00db10
 For example, the user might specify the locale @samp{espana-castellano}
00db10
 for most purposes, but specify the locale @samp{usa-english} for
00db10
@@ -120,7 +123,7 @@ which locales apply.  However, the user
00db10
 for a particular subset of those purposes.
00db10
 
00db10
 @node Locale Categories, Setting the Locale, Choosing Locale, Locales
00db10
-@section Categories of Activities that Locales Affect
00db10
+@section Locale Categories
00db10
 @cindex categories for locales
00db10
 @cindex locale categories
00db10
 
00db10
@@ -128,7 +131,11 @@ The purposes that locales serve are grou
00db10
 that a user or a program can choose the locale for each category
00db10
 independently.  Here is a table of categories; each name is both an
00db10
 environment variable that a user can set, and a macro name that you can
00db10
-use as an argument to @code{setlocale}.
00db10
+use as the first argument to @code{setlocale}.
00db10
+
00db10
+The contents of the environment variable (or the string in the second
00db10
+argument to @code{setlocale}) has to be a valid locale name.
00db10
+@xref{Locale Names}.
00db10
 
00db10
 @vtable @code
00db10
 @comment locale.h
00db10
@@ -172,7 +179,7 @@ for affirmative and negative responses.
00db10
 @comment locale.h
00db10
 @comment ISO
00db10
 @item LC_ALL
00db10
-This is not an environment variable; it is only a macro that you can use
00db10
+This is not a category; it is only a macro that you can use
00db10
 with @code{setlocale} to set a single locale for all purposes.  Setting
00db10
 this environment variable overwrites all selections by the other
00db10
 @code{LC_*} variables or @code{LANG}.
00db10
@@ -225,13 +232,7 @@ The symbols in this section are defined
00db10
 @comment ISO
00db10
 @deftypefun {char *} setlocale (int @var{category}, const char *@var{locale})
00db10
 The function @code{setlocale} sets the current locale for category
00db10
-@var{category} to @var{locale}.  A list of all the locales the system
00db10
-provides can be created by running
00db10
-
00db10
-@pindex locale
00db10
-@smallexample
00db10
-  locale -a
00db10
-@end smallexample
00db10
+@var{category} to @var{locale}.
00db10
 
00db10
 If @var{category} is @code{LC_ALL}, this specifies the locale for all
00db10
 purposes.  The other possible values of @var{category} specify an
00db10
@@ -256,10 +257,9 @@ is passed in as @var{locale} parameter.
00db10
 
00db10
 When you read the current locale for category @code{LC_ALL}, the value
00db10
 encodes the entire combination of selected locales for all categories.
00db10
-In this case, the value is not just a single locale name.  In fact, we
00db10
-don't make any promises about what it looks like.  But if you specify
00db10
-the same ``locale name'' with @code{LC_ALL} in a subsequent call to
00db10
-@code{setlocale}, it restores the same combination of locale selections.
00db10
+If you specify the same ``locale name'' with @code{LC_ALL} in a
00db10
+subsequent call to @code{setlocale}, it restores the same combination
00db10
+of locale selections.
00db10
 
00db10
 To be sure you can use the returned string encoding the currently selected
00db10
 locale at a later time, you must make a copy of the string.  It is not
00db10
@@ -275,20 +275,15 @@ for @var{category}.
00db10
 If a nonempty string is given for @var{locale}, then the locale of that
00db10
 name is used if possible.
00db10
 
00db10
+The effective locale name (either the second argument to
00db10
+@code{setlocale}, or if the argument is an empty string, the name
00db10
+obtained from the process environment) must be valid locale name.
00db10
+@xref{Locale Names}.
00db10
+
00db10
 If you specify an invalid locale name, @code{setlocale} returns a null
00db10
 pointer and leaves the current locale unchanged.
00db10
 @end deftypefun
00db10
 
00db10
-The path used for finding locale data can be set using the
00db10
-@code{LOCPATH} environment variable. The default path for finding
00db10
-locale data is system specific.  It is computed from the value given
00db10
-as the prefix while configuring the C library.  This value normally is
00db10
-@file{/usr} or @file{/}.  For the former the complete path is:
00db10
-
00db10
-@smallexample
00db10
-/usr/lib/locale
00db10
-@end smallexample
00db10
-
00db10
 Here is an example showing how you might use @code{setlocale} to
00db10
 temporarily switch to a new locale.
00db10
 
00db10
@@ -328,7 +323,7 @@ locale categories, and future versions o
00db10
 portability, assume that any symbol beginning with @samp{LC_} might be
00db10
 defined in @file{locale.h}.
00db10
 
00db10
-@node Standard Locales, Locale Information, Setting the Locale, Locales
00db10
+@node Standard Locales, Locale Names, Setting the Locale, Locales
00db10
 @section Standard Locales
00db10
 
00db10
 The only locale names you can count on finding on all operating systems
00db10
@@ -362,7 +357,94 @@ with the environment, rather than trying
00db10
 locale explicitly by name.  Remember, different machines might have
00db10
 different sets of locales installed.
00db10
 
00db10
-@node Locale Information, Formatting Numbers, Standard Locales, Locales
00db10
+@node Locale Names, Locale Information, Standard Locales, Locales
00db10
+@section Locale Names
00db10
+
00db10
+The following command prints a list of locales supported by the
00db10
+system:
00db10
+
00db10
+@pindex locale
00db10
+@smallexample
00db10
+  locale -a
00db10
+@end smallexample
00db10
+
00db10
+@strong{Portability Note:} With the notable exception of the standard
00db10
+locale names @samp{C} and @samp{POSIX}, locale names are
00db10
+system-specific.
00db10
+
00db10
+Most locale names follow XPG syntax and consist of up to four parts:
00db10
+
00db10
+@smallexample
00db10
+@var{language}[_@var{territory}[.@var{codeset}]][@@@var{modifier}]
00db10
+@end smallexample
00db10
+
00db10
+Beside the first part, all of them are allowed to be missing.  If the
00db10
+full specified locale is not found, less specific ones are looked for.
00db10
+The various parts will be stripped off, in the following order:
00db10
+
00db10
+@enumerate
00db10
+@item
00db10
+codeset
00db10
+@item
00db10
+normalized codeset
00db10
+@item
00db10
+territory
00db10
+@item
00db10
+modifier
00db10
+@end enumerate
00db10
+
00db10
+For example, the locale name @samp{de_AT.iso885915@@euro} denotes a
00db10
+German-language locale for use in Austria, using the ISO-8859-15
00db10
+(Latin-9) character set, and with the Euro as the currency symbol.
00db10
+
00db10
+In addition to locale names which follow XPG syntax, systems may
00db10
+provide aliases such as @samp{german}.  Both categories of names must
00db10
+not contain the slash character @samp{/}.
00db10
+
00db10
+If the locale name starts with a slash @samp{/}, it is treated as a
00db10
+path relative to the configured locale directories; see @code{LOCPATH}
00db10
+below.  The specified path must not contain a component @samp{..}, or
00db10
+the name is invalid, and @code{setlocale} will fail.
00db10
+
00db10
+@strong{Portability Note:} POSIX suggests that if a locale name starts
00db10
+with a slash @samp{/}, it is resolved as an absolute path.  However,
00db10
+@theglibc{} treats it as a relative path under the directories listed
00db10
+in @code{LOCPATH} (or the default locale directory if @code{LOCPATH}
00db10
+is unset).
00db10
+
00db10
+Locale names which are longer than an implementation-defined limit are
00db10
+invalid and cause @code{setlocale} to fail.
00db10
+
00db10
+As a special case, locale names used with @code{LC_ALL} can combine
00db10
+several locales, reflecting different locale settings for different
00db10
+categories.  For example, you might want to use a U.S. locale with ISO
00db10
+A4 paper format, so you set @code{LANG} to @samp{en_US.UTF-8}, and
00db10
+@code{LC_PAPER} to @samp{de_DE.UTF-8}.  In this case, the
00db10
+@code{LC_ALL}-style combined locale name is
00db10
+
00db10
+@smallexample
00db10
+LC_CTYPE=en_US.UTF-8;LC_TIME=en_US.UTF-8;LC_PAPER=de_DE.UTF-8;@dots{}
00db10
+@end smallexample
00db10
+
00db10
+followed by other category settings not shown here.
00db10
+
00db10
+@vindex LOCPATH
00db10
+The path used for finding locale data can be set using the
00db10
+@code{LOCPATH} environment variable.  This variable lists the
00db10
+directories in which to search for locale definitions, separated by a
00db10
+colon @samp{:}.
00db10
+
00db10
+The default path for finding locale data is system specific.  A typical
00db10
+value for the @code{LOCPATH} default is:
00db10
+
00db10
+@smallexample
00db10
+/usr/share/locale
00db10
+@end smallexample
00db10
+
00db10
+The value of @code{LOCPATH} is ignored by privileged programs for
00db10
+security reasons, and only the default directory is used.
00db10
+
00db10
+@node Locale Information, Formatting Numbers, Locale Names, Locales
00db10
 @section Accessing Locale Information
00db10
 
00db10
 There are several ways to access locale information.  The simplest