* lib/prototypes.h, configure.in, libmisc/Makefile.am,

libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetpwuid.c,
  libmisc/xgetgrnam.c, libmisc/xgetgrgid.c, libmisc/xgetspnam.c:
  Added functions xgetpwnam(), xgetpwuid(), xgetgrnam(),
  xgetgrgid(), and xgetspnam(). They allocate memory for the
  returned structure and are more robust to successive calls. They
  are implemented with the libc's getxxyyy_r() functions if
  available.
* libmisc/limits.c, libmisc/entry.c, libmisc/chowntty.c,
  libmisc/addgrps.c, libmisc/myname.c, libmisc/rlogin.c,
  libmisc/pwdcheck.c, src/newgrp.c, src/login_nopam.c,
  src/userdel.c, src/lastlog.c, src/grpck.c, src/gpasswd.c,
  src/newusers.c, src/chpasswd.c, src/chfn.c, src/groupmems.c,
  src/usermod.c, src/expiry.c, src/groupdel.c, src/chgpasswd.c,
  src/su.c, src/useradd.c, src/groupmod.c, src/passwd.c, src/pwck.c,
  src/groupadd.c, src/chage.c, src/login.c, src/suauth.c,
  src/faillog.c, src/groups.c, src/chsh.c, src/id.c: Review all the
  usage of one of the getpwnam(), getpwuid(), getgrnam(),
  getgrgid(), and getspnam() functions. It was noticed on
  http://bugs.debian.org/341230 that chfn and chsh use a passwd
  structure after calling a pam function, which result in using
  information from the passwd structure requested by pam, not the
  original one. It is much easier to use the new xget... functions
  to avoid these issues. I've checked which call to the original
  get... functions could be left (reducing the scope of the
  structure if possible), and I've left comments to ease future
  reviews (e.g. /* local, no need for xgetpwnam */).
  Note: the getpwent/getgrent calls should probably be checked also.
* src/groupdel.c, src/expiry.c: Fix typos in comments.
* src/groupmod.c: Re-indent.
* libmisc/Makefile.am, lib/groupmem.c, lib/groupio.c, lib/pwmem.c,
  lib/pwio.c, lib/shadowmem.c, lib/shadowio.c: Move the __<xx>_dup
  functions (used by the xget... functions) from the <xx>io.c files
  to the new <xx>mem.c files. This avoid linking some utils against
  the SELinux library.
This commit is contained in:
nekral-guest
2007-11-18 23:15:26 +00:00
parent ea63711c2c
commit 9adfc136b6
49 changed files with 721 additions and 261 deletions

View File

@@ -1,5 +1,5 @@
EXTRA_DIST = .indent.pro
EXTRA_DIST = .indent.pro xgetXXbyYY.c
INCLUDES = -I$(top_srcdir)/lib
@@ -49,4 +49,9 @@ libmisc_a_SOURCES = \
ulimit.c \
utmp.c \
valid.c \
xgetpwnam.c \
xgetpwuid.c \
xgetgrnam.c \
xgetgrgid.c \
xgetspnam.c \
xmalloc.c

View File

@@ -21,7 +21,6 @@ int add_groups (const char *list)
{
GETGROUPS_T *grouplist, *tmp;
int i, ngroups, added;
struct group *grp;
char *token;
char buf[1024];
@@ -50,8 +49,9 @@ int add_groups (const char *list)
added = 0;
for (token = strtok (buf, SEP); token; token = strtok (NULL, SEP)) {
struct group *grp;
grp = getgrnam (token);
grp = getgrnam (token); /* local, no need for xgetgrnam */
if (!grp) {
fprintf (stderr, _("Warning: unknown group %s\n"),
token);

View File

@@ -77,7 +77,7 @@ void chown_tty (const char *tty, const struct passwd *info)
gid = info->pw_gid;
else if (group[0] >= '0' && group[0] <= '9')
gid = atoi (group);
else if ((grent = getgrnam (group)))
else if ((grent = getgrnam (group))) /* local, no need for xgetgrnam */
gid = grent->gr_gid;
else
gid = info->pw_gid;

View File

@@ -43,7 +43,7 @@ void pw_entry (const char *name, struct passwd *pwent)
struct spwd *spwd;
if (!(passwd = getpwnam (name))) {
if (!(passwd = getpwnam (name))) { /* local, no need for xgetpwnam */
pwent->pw_name = (char *) 0;
return;
} else {
@@ -55,6 +55,7 @@ void pw_entry (const char *name, struct passwd *pwent)
pwent->pw_shell = xstrdup (passwd->pw_shell);
#if !defined(AUTOSHADOW)
setspent ();
/* local, no need for xgetspnam */
if ((spwd = getspnam (name))) {
pwent->pw_passwd = xstrdup (spwd->sp_pwdp);
endspent ();

View File

@@ -377,6 +377,7 @@ static void setup_usergroups (const struct passwd *info)
* (examples: 022 -> 002, 077 -> 007).
*/
if (info->pw_uid != 0 && info->pw_uid == info->pw_gid) {
/* local, no need for xgetgrgid */
grp = getgrgid (info->pw_gid);
if (grp && (strcmp (info->pw_name, grp->gr_name) == 0)) {
oldmask = umask (0777);

View File

@@ -31,8 +31,9 @@ struct passwd *get_my_pwent (void)
* XXX - when running from su, will return the current user (not
* the original user, like getlogin() does). Does this matter?
*/
if (cp && *cp && (pw = getpwnam (cp)) && pw->pw_uid == ruid)
if (cp && *cp && (pw = xgetpwnam (cp)) && pw->pw_uid == ruid)
return pw;
return getpwuid (ruid);
return xgetpwuid (ruid);
}

View File

@@ -46,7 +46,7 @@ void passwd_check (const char *user, const char *passwd, const char *progname)
struct spwd *sp;
if ((sp = getspnam (user)))
if ((sp = getspnam (user))) /* !USE_PAM, no need for xgetspnam */
passwd = sp->sp_pwdp;
endspent ();
if (pw_auth (passwd, user, PW_LOGIN, (char *) 0) != 0) {

View File

@@ -162,7 +162,7 @@ do_rlogin (const char *remote_host, char *name, int namelen, char *term,
#endif
STTY (0, &termio);
if (!(pwd = getpwnam (name)))
if (!(pwd = getpwnam (name))) /* local, no need for xgetpwnam */
return 0;
/*

109
libmisc/xgetXXbyYY.c Normal file
View File

@@ -0,0 +1,109 @@
/*
* According to the Linux-PAM documentation:
*
* 4.1. Care about standard library calls
*
* In general, writers of authorization-granting applications should
* assume that each module is likely to call any or all 'libc' functions.
* For 'libc' functions that return pointers to static/dynamically
* allocated structures (ie. the library allocates the memory and the
* user is not expected to 'free()' it) any module call to this function
* is likely to corrupt a pointer previously obtained by the application.
* The application programmer should either re-call such a 'libc'
* function after a call to the Linux-PAM library, or copy the structure
* contents to some safe area of memory before passing control to the
* Linux-PAM library.
*
* Two important function classes that fall into this category are
* getpwnam(3) and syslog(3).
*
* This file provide wrapper to the getpwnam or getpwnam_r functions.
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "config.h"
#include "prototypes.h"
#define XFUNCTION_NAME XPREFIX (FUNCTION_NAME)
#define XPREFIX(name) XPREFIX1 (name)
#define XPREFIX1(name) x##name
#define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
#define APPEND_R(name) APPEND_R1 (name)
#define APPEND_R1(name) name##_r
#define STRINGIZE(name) STRINGIZE1 (name)
#define STRINGIZE1(name) #name
LOOKUP_TYPE *XFUNCTION_NAME (ARG_TYPE ARG_NAME)
{
#if HAVE_FUNCTION_R
LOOKUP_TYPE *result=NULL;
char *buffer=NULL;
/* we have to start with something */
size_t length = 0x100;
result = malloc(sizeof(LOOKUP_TYPE));
if (NULL == result) {
fprintf (stderr, _("%s: out of memory\n"),
"x" STRINGIZE(FUNCTION_NAME));
exit (13);
}
do {
int status;
LOOKUP_TYPE *resbuf = NULL;
buffer = (char *)realloc (buffer, length);
if (NULL == buffer) {
fprintf (stderr, _("%s: out of memory\n"),
"x" STRINGIZE(FUNCTION_NAME));
exit (13);
}
errno = 0;
status = REENTRANT_NAME(ARG_NAME, result, buffer,
length, &resbuf);
if (!status && (resbuf == result)) {
/* Build a result structure that can be freed by
* the shadow *_free functions. */
LOOKUP_TYPE *ret_result = DUP_FUNCTION(result);
free(buffer);
free(result);
return ret_result;
}
if (ERANGE != errno) {
free (buffer);
free (result);
return NULL;
}
length *= 4;
} while (length < MAX_LENGTH);
free(buffer);
free(result);
return NULL;
#else /* !HAVE_FUNCTION_R */
/* No reentrant function.
* Duplicate the structure to avoid other call to overwrite it.
*
* We should also restore the initial structure. But that would be
* overkill.
*/
LOOKUP_TYPE *result = FUNCTION_NAME(ARG_NAME);
if (result) {
// result = DUP_FUNCTION(result);
if (NULL == result) {
fprintf (stderr, _("%s: out of memory\n"),
"x" STRINGIZE(FUNCTION_NAME));
exit (13);
}
}
return result;
#endif
}

34
libmisc/xgetgrgid.c Normal file
View File

@@ -0,0 +1,34 @@
/*
* According to the Linux-PAM documentation:
*
* 4.1. Care about standard library calls
*
* In general, writers of authorization-granting applications should
* assume that each module is likely to call any or all 'libc' functions.
* For 'libc' functions that return pointers to static/dynamically
* allocated structures (ie. the library allocates the memory and the
* user is not expected to 'free()' it) any module call to this function
* is likely to corrupt a pointer previously obtained by the application.
* The application programmer should either re-call such a 'libc'
* function after a call to the Linux-PAM library, or copy the structure
* contents to some safe area of memory before passing control to the
* Linux-PAM library.
*
* Two important function classes that fall into this category are
* getpwnam(3) and syslog(3).
*
* This file provide wrapper to the getpwnam or getpwnam_r functions.
*/
#include "groupio.h"
#define LOOKUP_TYPE struct group
#define FUNCTION_NAME getgrgid
#define ARG_TYPE gid_t
#define ARG_NAME gid
#define DUP_FUNCTION __gr_dup
#define MAX_LENGTH 0x8000
#define HAVE_FUNCTION_R (defined HAVE_GETGRGID_R)
#include "xgetXXbyYY.c"

34
libmisc/xgetgrnam.c Normal file
View File

@@ -0,0 +1,34 @@
/*
* According to the Linux-PAM documentation:
*
* 4.1. Care about standard library calls
*
* In general, writers of authorization-granting applications should
* assume that each module is likely to call any or all 'libc' functions.
* For 'libc' functions that return pointers to static/dynamically
* allocated structures (ie. the library allocates the memory and the
* user is not expected to 'free()' it) any module call to this function
* is likely to corrupt a pointer previously obtained by the application.
* The application programmer should either re-call such a 'libc'
* function after a call to the Linux-PAM library, or copy the structure
* contents to some safe area of memory before passing control to the
* Linux-PAM library.
*
* Two important function classes that fall into this category are
* getpwnam(3) and syslog(3).
*
* This file provide wrapper to the getpwnam or getpwnam_r functions.
*/
#include "groupio.h"
#define LOOKUP_TYPE struct group
#define FUNCTION_NAME getgrnam
#define ARG_TYPE const char *
#define ARG_NAME name
#define DUP_FUNCTION __gr_dup
#define MAX_LENGTH 0x8000
#define HAVE_FUNCTION_R (defined HAVE_GETGRNAM_R)
#include "xgetXXbyYY.c"

34
libmisc/xgetpwnam.c Normal file
View File

@@ -0,0 +1,34 @@
/*
* According to the Linux-PAM documentation:
*
* 4.1. Care about standard library calls
*
* In general, writers of authorization-granting applications should
* assume that each module is likely to call any or all 'libc' functions.
* For 'libc' functions that return pointers to static/dynamically
* allocated structures (ie. the library allocates the memory and the
* user is not expected to 'free()' it) any module call to this function
* is likely to corrupt a pointer previously obtained by the application.
* The application programmer should either re-call such a 'libc'
* function after a call to the Linux-PAM library, or copy the structure
* contents to some safe area of memory before passing control to the
* Linux-PAM library.
*
* Two important function classes that fall into this category are
* getpwnam(3) and syslog(3).
*
* This file provide wrapper to the getpwnam or getpwnam_r functions.
*/
#include "pwio.h"
#define LOOKUP_TYPE struct passwd
#define FUNCTION_NAME getpwnam
#define ARG_TYPE const char *
#define ARG_NAME name
#define DUP_FUNCTION __pw_dup
#define MAX_LENGTH 0x8000
#define HAVE_FUNCTION_R (defined HAVE_GETPWNAM_R)
#include "xgetXXbyYY.c"

34
libmisc/xgetpwuid.c Normal file
View File

@@ -0,0 +1,34 @@
/*
* According to the Linux-PAM documentation:
*
* 4.1. Care about standard library calls
*
* In general, writers of authorization-granting applications should
* assume that each module is likely to call any or all 'libc' functions.
* For 'libc' functions that return pointers to static/dynamically
* allocated structures (ie. the library allocates the memory and the
* user is not expected to 'free()' it) any module call to this function
* is likely to corrupt a pointer previously obtained by the application.
* The application programmer should either re-call such a 'libc'
* function after a call to the Linux-PAM library, or copy the structure
* contents to some safe area of memory before passing control to the
* Linux-PAM library.
*
* Two important function classes that fall into this category are
* getpwnam(3) and syslog(3).
*
* This file provide wrapper to the getpwnam or getpwnam_r functions.
*/
#include "pwio.h"
#define LOOKUP_TYPE struct passwd
#define FUNCTION_NAME getpwuid
#define ARG_TYPE uid_t
#define ARG_NAME uid
#define DUP_FUNCTION __pw_dup
#define MAX_LENGTH 0x8000
#define HAVE_FUNCTION_R (defined HAVE_GETPWUID_R)
#include "xgetXXbyYY.c"

32
libmisc/xgetspnam.c Normal file
View File

@@ -0,0 +1,32 @@
/*
* According to the Linux-PAM documentation:
*
* 4.1. Care about standard library calls
*
* In general, writers of authorization-granting applications should
* assume that each module is likely to call any or all 'libc' functions.
* For 'libc' functions that return pointers to static/dynamically
* allocated structures (ie. the library allocates the memory and the
* user is not expected to 'free()' it) any module call to this function
* is likely to corrupt a pointer previously obtained by the application.
* The application programmer should either re-call such a 'libc'
* function after a call to the Linux-PAM library, or copy the structure
* contents to some safe area of memory before passing control to the
* Linux-PAM library.
*
* Two important function classes that fall into this category are
* getpwnam(3) and syslog(3).
*
* This file provide wrapper to the getpwnam or getpwnam_r functions.
*/
#define LOOKUP_TYPE struct spwd
#define FUNCTION_NAME getspnam
#define ARG_TYPE const char *
#define ARG_NAME name
#define DUP_FUNCTION __spw_dup
#define MAX_LENGTH 0x8000
#define HAVE_FUNCTION_R (defined HAVE_GETSPNAM_R)
#include "xgetXXbyYY.c"