PR/50454: Timo Buhrmester: Fix wrong allocation for wall(1) -g

Modernize.
This commit is contained in:
christos 2015-11-21 14:59:51 +00:00
parent a84a9bb93c
commit 2ea5bc272a
2 changed files with 73 additions and 64 deletions

View File

@ -1,7 +1,8 @@
# $NetBSD: Makefile,v 1.10 2007/05/28 12:06:32 tls Exp $ # $NetBSD: Makefile,v 1.11 2015/11/21 14:59:51 christos Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93 # @(#)Makefile 8.1 (Berkeley) 6/6/93
.include <bsd.own.mk> .include <bsd.own.mk>
WARNS=6
USE_FORT?= yes # setuid USE_FORT?= yes # setuid
PROG= wall PROG= wall

View File

@ -1,4 +1,4 @@
/* $NetBSD: wall.c,v 1.29 2011/09/06 18:45:21 joerg Exp $ */ /* $NetBSD: wall.c,v 1.30 2015/11/21 14:59:51 christos Exp $ */
/* /*
* Copyright (c) 1988, 1990, 1993 * Copyright (c) 1988, 1990, 1993
@ -39,7 +39,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1990, 1993\
#if 0 #if 0
static char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93"; static char sccsid[] = "@(#)wall.c 8.2 (Berkeley) 11/16/93";
#endif #endif
__RCSID("$NetBSD: wall.c,v 1.29 2011/09/06 18:45:21 joerg Exp $"); __RCSID("$NetBSD: wall.c,v 1.30 2015/11/21 14:59:51 christos Exp $");
#endif /* not lint */ #endif /* not lint */
/* /*
@ -68,7 +68,7 @@ __RCSID("$NetBSD: wall.c,v 1.29 2011/09/06 18:45:21 joerg Exp $");
#include "term_chk.h" #include "term_chk.h"
static void addgroup(char *); static void addgroup(char *);
static void makemsg(const char *); static void makemsg(struct iovec *, const char *, int);
__dead static void usage(void); __dead static void usage(void);
static struct wallgroup { static struct wallgroup {
@ -78,11 +78,6 @@ static struct wallgroup {
struct wallgroup *next; struct wallgroup *next;
} *grouplist; } *grouplist;
static int nobanner;
static size_t mbufsize;
static char *mbuf;
/* ARGSUSED */
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -93,15 +88,19 @@ main(int argc, char **argv)
gid_t egid; gid_t egid;
struct wallgroup *wg; struct wallgroup *wg;
struct passwd *pw; struct passwd *pw;
int nobanner;
setprogname(argv[0]); setprogname(argv[0]);
egid = getegid(); egid = getegid();
if (setegid(getgid()) == -1) if (setegid(getgid()) == -1)
err(1, "setegid"); err(EXIT_FAILURE, "setegid");
pw = getpwnam("nobody"); pw = getpwnam("nobody");
if (pw == NULL)
errx(EXIT_FAILURE, "Can't find passwd entry for `nobody'");
(void)check_sender(NULL, getuid(), egid); (void)check_sender(NULL, getuid(), egid);
nobanner = 0;
while ((ch = getopt(argc, argv, "g:n")) != -1) while ((ch = getopt(argc, argv, "g:n")) != -1)
switch (ch) { switch (ch) {
case 'n': case 'n':
@ -121,10 +120,8 @@ main(int argc, char **argv)
if (argc > 1) if (argc > 1)
usage(); usage();
makemsg(*argv); makemsg(&iov, *argv, nobanner);
iov.iov_base = mbuf;
iov.iov_len = mbufsize;
(void)getutentries(NULL, &ep); (void)getutentries(NULL, &ep);
(void)setegid(egid); (void)setegid(egid);
for (; ep; ep = ep->next) { for (; ep; ep = ep->next) {
@ -153,34 +150,34 @@ main(int argc, char **argv)
if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL) if ((p = ttymsg(&iov, 1, ep->line, 60*5)) != NULL)
warnx("%s", p); warnx("%s", p);
} }
exit(0); return EXIT_SUCCESS;
} }
static void static void
addgroup(char *name) addgroup(char *name)
{ {
int i; size_t i;
struct group *grp; struct group *grp;
struct wallgroup *g; struct wallgroup *g;
grp = getgrnam(name); grp = getgrnam(name);
if ((grp = getgrnam(name)) == NULL) if ((grp = getgrnam(name)) == NULL)
errx(1, "unknown group `%s'", name); errx(EXIT_FAILURE, "unknown group `%s'", name);
for (i = 0; grp->gr_mem[i]; i++) for (i = 0; grp->gr_mem[i]; i++)
continue; continue;
g = (struct wallgroup *)malloc(sizeof *g); g = malloc(sizeof(*g));
if (g == NULL) if (g == NULL)
err(1, "malloc"); err(EXIT_FAILURE, "malloc");
g->gid = grp->gr_gid; g->gid = grp->gr_gid;
g->name = name; g->name = name;
g->mem = (char **)malloc(i + 1); g->mem = calloc(i + 1, sizeof(*g->mem));
if (g->mem == NULL) if (g->mem == NULL)
err(1, "malloc"); err(EXIT_FAILURE, "calloc");
for (i = 0; grp->gr_mem[i] != NULL; i++) { for (i = 0; grp->gr_mem[i] != NULL; i++) {
g->mem[i] = strdup(grp->gr_mem[i]); g->mem[i] = strdup(grp->gr_mem[i]);
if (g->mem[i] == NULL) if (g->mem[i] == NULL)
err(1, "malloc"); err(EXIT_FAILURE, "strdup");
} }
g->mem[i] = NULL; g->mem[i] = NULL;
g->next = grouplist; g->next = grouplist;
@ -188,57 +185,66 @@ addgroup(char *name)
} }
static void static void
makemsg(const char *fname) makebanner(FILE *fp)
{ {
int ch, cnt; const char *whom, *tty;
char hostname[MAXHOSTNAMELEN + 1], lbuf[100];
time_t now;
struct tm *lt; struct tm *lt;
struct passwd *pw; struct passwd *pw;
if (!(whom = getlogin()))
whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
(void)gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
(void)time(&now);
lt = localtime(&now);
/*
* all this stuff is to blank out a square for the message;
* we wrap message lines at column 79, not 80, because some
* terminals wrap after 79, some do not, and we can't tell.
* Which means that we may leave a non-blank character
* in column 80, but that can't be helped.
*/
(void)fprintf(fp, "\r%79s\r\n", " ");
(void)snprintf(lbuf, sizeof lbuf,
"Broadcast Message from %s@%s", whom, hostname);
(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
tty = ttyname(STDERR_FILENO);
if (tty == NULL)
tty = "??";
(void)snprintf(lbuf, sizeof lbuf, " (%s) at %d:%02d %s...", tty,
lt->tm_hour, lt->tm_min, lt->tm_zone);
(void)fprintf(fp, "%-79.79s\r\n", lbuf);
}
static void
makemsg(struct iovec *iov, const char *fname, int nobanner)
{
int ch, cnt;
struct stat sbuf; struct stat sbuf;
time_t now;
FILE *fp; FILE *fp;
int fd; int fd;
const char *whom, *tty; char *p, tmpname[MAXPATHLEN], lbuf[100];
char *p, tmpname[MAXPATHLEN], lbuf[100], size_t mbufsize;
hostname[MAXHOSTNAMELEN+1]; char *mbuf;
(void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP); (void)snprintf(tmpname, sizeof tmpname, "%s/wall.XXXXXX", _PATH_TMP);
if ((fd = mkstemp(tmpname)) == -1) if ((fd = mkstemp(tmpname)) == -1)
err(1, "can't open temporary file"); err(EXIT_FAILURE, "can't open temporary file");
(void)unlink(tmpname); (void)unlink(tmpname);
if (!(fp = fdopen(fd, "r+"))) if (!(fp = fdopen(fd, "r+")))
err(1, "can't open temporary file"); err(EXIT_FAILURE, "can't open temporary file");
if (!nobanner) { if (!nobanner)
if (!(whom = getlogin())) makebanner(fp);
whom = (pw = getpwuid(getuid())) ? pw->pw_name : "???";
(void)gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
(void)time(&now);
lt = localtime(&now);
/*
* all this stuff is to blank out a square for the message;
* we wrap message lines at column 79, not 80, because some
* terminals wrap after 79, some do not, and we can't tell.
* Which means that we may leave a non-blank character
* in column 80, but that can't be helped.
*/
(void)fprintf(fp, "\r%79s\r\n", " ");
(void)snprintf(lbuf, sizeof lbuf,
"Broadcast Message from %s@%s", whom, hostname);
(void)fprintf(fp, "%-79.79s\007\007\r\n", lbuf);
tty = ttyname(STDERR_FILENO);
if (tty == NULL)
tty = "??";
(void)snprintf(lbuf, sizeof lbuf,
" (%s) at %d:%02d %s...", tty,
lt->tm_hour, lt->tm_min, lt->tm_zone);
(void)fprintf(fp, "%-79.79s\r\n", lbuf);
}
(void)fprintf(fp, "%79s\r\n", " "); (void)fprintf(fp, "%79s\r\n", " ");
if (fname && !(freopen(fname, "r", stdin))) if (fname && !(freopen(fname, "r", stdin)))
err(1, "can't read %s", fname); err(EXIT_FAILURE, "can't read %s", fname);
while (fgets(lbuf, sizeof(lbuf), stdin)) while (fgets(lbuf, sizeof(lbuf), stdin))
for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) { for (cnt = 0, p = lbuf; (ch = *p) != '\0'; ++p, ++cnt) {
if (cnt == 79 || ch == '\n') { if (cnt == 79 || ch == '\n') {
@ -254,22 +260,24 @@ makemsg(const char *fname)
(void)fprintf(fp, "%79s\r\n", " "); (void)fprintf(fp, "%79s\r\n", " ");
rewind(fp); rewind(fp);
if (fstat(fd, &sbuf)) if (fstat(fd, &sbuf) == -1)
err(1, "can't stat temporary file"); err(EXIT_FAILURE, "can't stat temporary file");
if ((uint64_t)sbuf.st_size > SIZE_T_MAX) if ((uint64_t)sbuf.st_size > SIZE_T_MAX)
errx(1, "file too big"); errx(EXIT_FAILURE, "file too big");
mbufsize = sbuf.st_size; mbufsize = (size_t)sbuf.st_size;
if (!(mbuf = malloc(mbufsize))) if (!(mbuf = malloc(mbufsize)))
err(1, "malloc"); err(EXIT_FAILURE, "malloc");
if (fread(mbuf, 1, mbufsize, fp) != mbufsize) if (fread(mbuf, 1, mbufsize, fp) != mbufsize)
err(1, "can't read temporary file"); err(EXIT_FAILURE, "can't read temporary file");
(void)fclose(fp); (void)fclose(fp);
iov->iov_base = mbuf;
iov->iov_len = mbufsize;
} }
static void static void
usage(void) usage(void)
{ {
(void)fprintf(stderr, "usage: %s [-g group] [file]\n", getprogname()); (void)fprintf(stderr, "Usage: %s [-g group] [file]\n", getprogname());
exit(1); exit(EXIT_FAILURE);
} }