Bug 4416 - setlocale can fail silently
Summary: setlocale can fail silently
Status: RESOLVED INVALID
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-04-23 22:58 UTC by Pierre Habouzit
Modified: 2014-07-05 07:01 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pierre Habouzit 2007-04-23 22:58:58 UTC
When Virtual Memory is low, the mmap of /usr/lib/locale/locale-archive can fail 
(in loadarchive.c, function _nl_load_locale_from_archive).

Then, _nl_find_locale (findlocale.c) tries to recover from that, trying to 
guess pathes or sth like that on its own. But it still fails because it can't 
find LC_IDENTIFICATION files and setlocale returns something completely broken.

You can emulate that by renaming /usr/lib/locale/locale-archive. A sample 
problematic program is:

================================================
#include <stdio.h>
#include <locale.h>

int main(void)
{
    const char *s;

    s = setlocale(LC_ALL, "en_US.utf-8");
    printf("%s\n", s);
    return 0;
}
================================================


strace gives:

================================================
brk(0)                                  = 0x601000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 
0x2ac0f56dd000
uname({sys="Linux", node="artemis", ...}) = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 
0x2ac0f56de000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=57422, ...}) = 0
mmap(NULL, 57422, PROT_READ, MAP_PRIVATE, 3, 0) = 0x2ac0f56e0000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/libc.so.6", O_RDONLY)        = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\20\324\1"..., 832) = 
832
fstat(3, {st_mode=S_IFREG|0755, st_size=1359200, ...}) = 0
mmap(NULL, 2416808, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 
0x2ac0f57de000
mprotect(0x2ac0f5924000, 1044480, PROT_NONE) = 0
mmap(0x2ac0f5a23000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|
MAP_DENYWRITE, 3, 0x145000) = 0x2ac0f5a23000
mmap(0x2ac0f5a28000, 16552, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|
MAP_ANONYMOUS, -1, 0) = 0x2ac0f5a28000
close(3)                                = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 
0x2ac0f5a2d000
arch_prctl(ARCH_SET_FS, 0x2ac0f5a2d6f0) = 0
mprotect(0x2ac0f5a23000, 12288, PROT_READ) = 0
munmap(0x2ac0f56e0000, 57422)           = 0
brk(0)                                  = 0x601000
brk(0x622000)                           = 0x622000
open("/usr/lib/locale/locale-archive", O_RDONLY) = -1 ENOENT (No such file or 
directory)
open("/usr/share/locale/locale.alias", O_RDONLY) = -1 ENOENT (No such file or 
directory)
open("/usr/lib/locale/en_US.utf-8/LC_IDENTIFICATION", O_RDONLY) = -1 ENOENT (No 
such file or directory)
open("/usr/lib/locale/en_US.utf8/LC_IDENTIFICATION", O_RDONLY) = -1 ENOENT (No 
such file or directory)
open("/usr/lib/locale/en_US/LC_IDENTIFICATION", O_RDONLY) = -1 ENOENT (No such 
file or directory)
open("/usr/lib/locale/en.utf-8/LC_IDENTIFICATION", O_RDONLY) = -1 ENOENT (No 
such file or directory)
open("/usr/lib/locale/en.utf8/LC_IDENTIFICATION", O_RDONLY) = -1 ENOENT (No 
such file or directory)
open("/usr/lib/locale/en/LC_IDENTIFICATION", O_RDONLY) = -1 ENOENT (No such 
file or directory)
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
====================================================

I don't expect setlocale especially to success even with low memory, but at 
least it should fail (return NULL) to express the fact that it only loaded C 
locale.
Comment 1 Pierre Habouzit 2007-04-24 11:14:33 UTC
ahem, I was testing on a 2.3 libc rather than the 2.5 sorry, it seems to work 
fine now.