From 7232f895edaddb02c7c3cda04eba56b6d48bb154 Mon Sep 17 00:00:00 2001 From: jbj Date: Thu, 17 Apr 2003 15:14:49 +0000 Subject: [PATCH] Don't overflow internal tables with unknown user/group (#85346). CVS patchset: 6758 CVS date: 2003/04/17 15:14:49 --- build/names.c | 90 +++++++++++++++++++++++++++++++++++--------------------- build/rpmbuild.h | 24 +++++++++++---- 2 files changed, 75 insertions(+), 39 deletions(-) diff --git a/build/names.c b/build/names.c index 3743cfc..d37591a 100644 --- a/build/names.c +++ b/build/names.c @@ -39,6 +39,8 @@ void freeNames(void) /*@-boundswrite@*/ const char *getUname(uid_t uid) + /*@globals uid_used, uids, unames @*/ + /*@modifies uid_used, uids, unames @*/ { struct passwd *pw; int x; @@ -52,17 +54,19 @@ const char *getUname(uid_t uid) /* XXX - This is the other hard coded limit */ if (x == 1024) rpmlog(RPMLOG_CRIT, _("getUname: too many uid's\n")); - uid_used++; - pw = getpwuid(uid); - uids[x] = uid; - unames[x] = (pw ? xstrdup(pw->pw_name) : NULL); - return unames[x]; + if ((pw = getpwuid(uid)) == NULL) + return NULL; + uids[uid_used] = uid; + unames[uid_used] = xstrdup(pw->pw_name); + return unames[uid_used++]; } /*@=boundswrite@*/ /*@-boundswrite@*/ const char *getUnameS(const char *uname) + /*@globals uid_used, uids, unames @*/ + /*@modifies uid_used, uids, unames @*/ { struct passwd *pw; int x; @@ -76,17 +80,22 @@ const char *getUnameS(const char *uname) /* XXX - This is the other hard coded limit */ if (x == 1024) rpmlog(RPMLOG_CRIT, _("getUnameS: too many uid's\n")); - uid_used++; - pw = getpwnam(uname); - uids[x] = (pw ? pw->pw_uid : -1); - unames[x] = (pw ? xstrdup(pw->pw_name) : xstrdup(uname)); - return unames[x]; + if ((pw = getpwnam(uname)) == NULL) { + uids[uid_used] = -1; + unames[uid_used] = xstrdup(uname); + } else { + uids[uid_used] = pw->pw_uid; + unames[uid_used] = xstrdup(pw->pw_name); + } + return unames[uid_used++]; } /*@=boundswrite@*/ /*@-boundswrite@*/ uid_t getUidS(const char *uname) + /*@globals uid_used, uids, unames @*/ + /*@modifies uid_used, uids, unames @*/ { struct passwd *pw; int x; @@ -100,17 +109,22 @@ uid_t getUidS(const char *uname) /* XXX - This is the other hard coded limit */ if (x == 1024) rpmlog(RPMLOG_CRIT, _("getUidS: too many uid's\n")); - uid_used++; - pw = getpwnam(uname); - uids[x] = (pw ? pw->pw_uid : -1); - unames[x] = (pw ? xstrdup(pw->pw_name) : xstrdup(uname)); - return uids[x]; + if ((pw = getpwnam(uname)) == NULL) { + uids[uid_used] = -1; + unames[uid_used] = xstrdup(uname); + } else { + uids[uid_used] = pw->pw_uid; + unames[uid_used] = xstrdup(pw->pw_name); + } + return uids[uid_used++]; } /*@=boundswrite@*/ /*@-boundswrite@*/ const char *getGname(gid_t gid) + /*@globals gid_used, gids, gnames @*/ + /*@modifies gid_used, gids, gnames @*/ { struct group *gr; int x; @@ -124,17 +138,19 @@ const char *getGname(gid_t gid) /* XXX - This is the other hard coded limit */ if (x == 1024) rpmlog(RPMLOG_CRIT, _("getGname: too many gid's\n")); - gid_used++; - gr = getgrgid(gid); - gids[x] = gid; - gnames[x] = (gr ? xstrdup(gr->gr_name) : NULL); - return gnames[x]; + if ((gr = getgrgid(gid)) == NULL) + return NULL; + gids[gid_used] = gid; + gnames[gid_used] = xstrdup(gr->gr_name); + return gnames[gid_used++]; } /*@=boundswrite@*/ /*@-boundswrite@*/ const char *getGnameS(const char *gname) + /*@globals gid_used, gids, gnames @*/ + /*@modifies gid_used, gids, gnames @*/ { struct group *gr; int x; @@ -148,17 +164,22 @@ const char *getGnameS(const char *gname) /* XXX - This is the other hard coded limit */ if (x == 1024) rpmlog(RPMLOG_CRIT, _("getGnameS: too many gid's\n")); - gid_used++; - gr = getgrnam(gname); - gids[x] = (gr ? gr->gr_gid : -1); - gnames[x] = (gr ? xstrdup(gr->gr_name) : xstrdup(gname)); - return gnames[x]; + if ((gr = getgrnam(gname)) == NULL) { + gids[gid_used] = -1; + gnames[gid_used] = xstrdup(gname); + } else { + gids[gid_used] = gr->gr_gid; + gnames[gid_used] = xstrdup(gr->gr_name); + } + return gnames[gid_used++]; } /*@=boundswrite@*/ /*@-boundswrite@*/ gid_t getGidS(const char *gname) + /*@globals gid_used, gids, gnames @*/ + /*@modifies gid_used, gids, gnames @*/ { struct group *gr; int x; @@ -172,12 +193,15 @@ gid_t getGidS(const char *gname) /* XXX - This is the other hard coded limit */ if (x == 1024) rpmlog(RPMLOG_CRIT, _("getGidS: too many gid's\n")); - gid_used++; - gr = getgrnam(gname); - gids[x] = (gr ? gr->gr_gid : -1); - gnames[x] = (gr ? xstrdup(gr->gr_name) : xstrdup(gname)); - return gids[x]; + if ((gr = getgrnam(gname)) == NULL) { + gids[gid_used] = -1; + gnames[gid_used] = xstrdup(gname); + } else { + gids[gid_used] = gr->gr_gid; + gnames[gid_used] = xstrdup(gr->gr_name); + } + return gids[gid_used++]; } /*@=boundswrite@*/ @@ -196,10 +220,10 @@ int_32 *const getBuildTime(void) const char *const buildHost(void) { static char hostname[1024]; - static int gotit = 0; + static int oneshot = 0; struct hostent *hbn; - if (! gotit) { + if (! oneshot) { (void) gethostname(hostname, sizeof(hostname)); /*@-unrecog -multithreaded @*/ /*@-globs@*/ /* FIX: h_errno access */ @@ -211,7 +235,7 @@ const char *const buildHost(void) else rpmMessage(RPMMESS_WARNING, _("Could not canonicalize hostname: %s\n"), hostname); - gotit = 1; + oneshot = 1; } return(hostname); } diff --git a/build/rpmbuild.h b/build/rpmbuild.h index b4070f4..44d1996 100644 --- a/build/rpmbuild.h +++ b/build/rpmbuild.h @@ -94,7 +94,9 @@ void freeNames(void) * @param uid user id * @return cached user name */ -extern /*@observer@*/ const char * getUname(uid_t uid) /*@*/; +extern /*@observer@*/ const char * getUname(uid_t uid) + /*@globals internalState @*/ + /*@modifies internalState @*/; /** \ingroup rpmbuild * Return cached user name. @@ -102,7 +104,9 @@ extern /*@observer@*/ const char * getUname(uid_t uid) /*@*/; * @param uname user name * @return cached user name */ -extern /*@observer@*/ const char * getUnameS(const char * uname) /*@*/; +extern /*@observer@*/ const char * getUnameS(const char * uname) + /*@globals internalState @*/ + /*@modifies internalState @*/; /** \ingroup rpmbuild * Return cached user id. @@ -110,7 +114,9 @@ extern /*@observer@*/ const char * getUnameS(const char * uname) /*@*/; * @param uname user name * @return cached uid */ -uid_t getUidS(const char * uname) /*@*/; +uid_t getUidS(const char * uname) + /*@globals internalState @*/ + /*@modifies internalState @*/; /** \ingroup rpmbuild * Return cached group name from group id. @@ -118,7 +124,9 @@ uid_t getUidS(const char * uname) /*@*/; * @param gid group id * @return cached group name */ -extern /*@observer@*/ const char * getGname(gid_t gid) /*@*/; +extern /*@observer@*/ const char * getGname(gid_t gid) + /*@globals internalState @*/ + /*@modifies internalState @*/; /** \ingroup rpmbuild * Return cached group name. @@ -126,7 +134,9 @@ extern /*@observer@*/ const char * getGname(gid_t gid) /*@*/; * @param gname group name * @return cached group name */ -extern /*@observer@*/ const char * getGnameS(const char * gname) /*@*/; +extern /*@observer@*/ const char * getGnameS(const char * gname) + /*@globals internalState @*/ + /*@modifies internalState @*/; /** \ingroup rpmbuild * Return cached group id. @@ -134,7 +144,9 @@ extern /*@observer@*/ const char * getGnameS(const char * gname) /*@*/; * @param gname group name * @return cached gid */ -gid_t getGidS(const char * gname) /*@*/; +gid_t getGidS(const char * gname) + /*@globals internalState @*/ + /*@modifies internalState @*/; /** \ingroup rpmbuild * Return build hostname. -- 2.7.4