Handle too-small buffers in Linux getlogin_r.
[glibc.git] / sysdeps / unix / sysv / linux / getlogin_r.c
blobdad2671e802bf64f006e53c3deb0aab26b0242ce
1 /* Copyright (C) 2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <pwd.h>
20 #include <unistd.h>
21 #include <not-cancel.h>
23 #define STATIC static
24 static int getlogin_r_fd0 (char *name, size_t namesize);
25 #define getlogin_r getlogin_r_fd0
26 #include <sysdeps/unix/getlogin_r.c>
27 #undef getlogin_r
30 int
31 attribute_hidden
32 __getlogin_r_loginuid (name, namesize)
33 char *name;
34 size_t namesize;
36 int fd = open_not_cancel_2 ("/proc/self/loginuid", O_RDONLY);
37 if (fd == -1)
38 return 1;
40 /* We are reading a 32-bit number. 12 bytes are enough for the text
41 representation. If not, something is wrong. */
42 char uidbuf[12];
43 ssize_t n = TEMP_FAILURE_RETRY (read_not_cancel (fd, uidbuf,
44 sizeof (uidbuf)));
45 close_not_cancel_no_status (fd);
47 uid_t uid;
48 char *endp;
49 if (n <= 0
50 || n == sizeof (uidbuf)
51 || (uidbuf[n] = '\0',
52 uid = strtoul (uidbuf, &endp, 10),
53 endp == uidbuf || *endp != '\0'))
54 return 1;
56 size_t buflen = 1024;
57 char *buf = alloca (buflen);
58 bool use_malloc = false;
59 struct passwd pwd;
60 struct passwd *tpwd;
61 int res;
63 while ((res = __getpwuid_r (uid, &pwd, buf, buflen, &tpwd)) != 0)
64 if (__libc_use_alloca (2 * buflen))
65 extend_alloca (buf, buflen, 2 * buflen);
66 else
68 buflen *= 2;
69 char *newp = realloc (use_malloc ? buf : NULL, buflen);
70 if (newp == NULL)
72 fail:
73 if (use_malloc)
74 free (buf);
75 return 1;
77 buf = newp;
78 use_malloc = true;
81 if (tpwd == NULL)
82 goto fail;
84 int result = 0;
85 size_t needed = strlen (pwd.pw_name) + 1;
86 if (needed > namesize)
88 __set_errno (ERANGE);
89 result = ERANGE;
90 goto out;
93 memcpy (name, pwd.pw_name, needed);
95 out:
96 if (use_malloc)
97 free (buf);
99 return result;
103 /* Return at most NAME_LEN characters of the login name of the user in NAME.
104 If it cannot be determined or some other error occurred, return the error
105 code. Otherwise return 0. */
108 getlogin_r (name, namesize)
109 char *name;
110 size_t namesize;
112 if (__getlogin_r_loginuid (name, namesize) == 0)
113 return 0;
115 return getlogin_r_fd0 (name, namesize);
117 libc_hidden_def (getlogin_r)