aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author <doug@openbsd.org>2015-07-18 00:19:38 +0000
committer <doug@openbsd.org>2015-07-18 00:19:38 +0000
commitdba0eda8cebb119477002ef8c1d1c6463c0dccb6 (patch)
treea7015d6107edef951c1ec8e4a3e935ed35a8d0d0
parentc6b63a4712f2c5652a0b246d200330a36ce32020 (diff)
downloadopendoas-dba0eda8cebb119477002ef8c1d1c6463c0dccb6.tar.gz
Add more error checking and use named constants when useful.
ok sthen@ tedu@
-rw-r--r--doas.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/doas.c b/doas.c
index 47ae35a..31222a6 100644
--- a/doas.c
+++ b/doas.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: doas.c,v 1.5 2015/07/16 22:11:01 nicm Exp $ */
+/* $OpenBSD: doas.c,v 1.6 2015/07/16 23:22:08 nicm Exp $ */
/*
* Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
*
@@ -244,8 +244,8 @@ fail(void)
int
main(int argc, char **argv, char **envp)
{
- char cmdline[1024];
- char myname[32];
+ char cmdline[LINE_MAX];
+ char myname[_PW_NAME_LEN + 1];
uid_t uid, target = 0;
gid_t groups[NGROUPS_MAX + 1];
int ngroups;
@@ -276,17 +276,21 @@ main(int argc, char **argv, char **envp)
usage();
cmd = argv[0];
- strlcpy(cmdline, argv[0], sizeof(cmdline));
+ if (strlcpy(cmdline, argv[0], sizeof(cmdline)) >= sizeof(cmdline))
+ errx(1, "command line too long");
for (i = 1; i < argc; i++) {
- strlcat(cmdline, " ", sizeof(cmdline));
- strlcat(cmdline, argv[i], sizeof(cmdline));
+ if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
+ errx(1, "command line too long");
+ if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
+ errx(1, "command line too long");
}
uid = getuid();
pw = getpwuid(uid);
if (!pw)
err(1, "getpwuid failed");
- strlcpy(myname, pw->pw_name, sizeof(myname));
+ if (strlcpy(myname, pw->pw_name, sizeof(myname)) >= sizeof(myname))
+ errx(1, "pw_name too long");
ngroups = getgroups(NGROUPS_MAX, groups);
if (ngroups == -1)
err(1, "can't get groups");
@@ -317,7 +321,8 @@ main(int argc, char **argv, char **envp)
syslog(LOG_AUTHPRIV | LOG_INFO, "%s ran command as %s: %s",
myname, pw->pw_name, cmdline);
- setenv("PATH", safepath, 1);
+ if (setenv("PATH", safepath, 1) == -1)
+ err(1, "failed to set PATH '%s'", safepath);
execvpe(cmd, argv, envp);
err(1, "%s", cmd);
}