arrfab / rpms / glibc

Forked from rpms/glibc 4 years ago
Clone

Blame SOURCES/glibc-rh1133812-2.patch

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