From 5d57dad57ace72e555acc12929c27c1ba2123f7a Mon Sep 17 00:00:00 2001 From: Michiel Broek Date: Tue, 30 Aug 2005 17:53:35 +0000 Subject: [PATCH] Secured sprintf with snprintf --- unix/env.c | 4 ++-- unix/getdef.c | 4 ++-- unix/loginprompt.c | 4 ++-- unix/mblogin.c | 2 +- unix/mbpasswd.c | 10 +++++----- unix/mbuseradd.c | 18 +++++++++--------- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/unix/env.c b/unix/env.c index dc2f73a7..5d1b13ae 100644 --- a/unix/env.c +++ b/unix/env.c @@ -6,7 +6,7 @@ * Original Copyright ....: Julianne Frances Haugh and others. * ***************************************************************************** - * Copyright (C) 1997-2001 + * Copyright (C) 1997-2005 * * Michiel Broek FIDO: 2:280/2802 * Beekmansbos 10 @@ -98,7 +98,7 @@ void addenv(const char *string, const char *value) if (value) { newstring = xmalloc(strlen(string) + strlen(value) + 2); - sprintf(newstring, "%s=%s", string, value); + snprintf(newstring, strlen(string) + strlen(value) + 2, "%s=%s", string, value); } else { newstring = xstrdup(string); } diff --git a/unix/getdef.c b/unix/getdef.c index 48ac74c0..97eb7e32 100644 --- a/unix/getdef.c +++ b/unix/getdef.c @@ -6,7 +6,7 @@ * Original Copyright ....: Julianne Frances Haugh and others. * ***************************************************************************** - * Copyright (C) 1997-2002 + * Copyright (C) 1997-2005 * * Michiel Broek FIDO: 2:280/2802 * Beekmansbos 10 @@ -243,7 +243,7 @@ void def_load(void) syslog(LOG_CRIT, "cannot find user `mbse' in password file"); return; } - sprintf(def_fname, "%s/etc/login.defs", pw->pw_dir); + snprintf(def_fname, PATH_MAX, "%s/etc/login.defs", pw->pw_dir); /* * Open the configuration definitions file. diff --git a/unix/loginprompt.c b/unix/loginprompt.c index 5ce7755f..a950f1a4 100644 --- a/unix/loginprompt.c +++ b/unix/loginprompt.c @@ -6,7 +6,7 @@ * Original Copyright ....: Julianne Frances Haugh and others. * ***************************************************************************** - * Copyright (C) 1997-2002 + * Copyright (C) 1997-2005 * * Michiel Broek FIDO: 2:280/2802 * Beekmansbos 10 @@ -151,7 +151,7 @@ void login_prompt(const char *prompt, char *name, int namesize) envp[envc] = nvar; } else { envp[envc] = xmalloc(strlen(nvar) + 32); - sprintf(envp[envc], "L%d=%s", count++, nvar); + snprintf(envp[envc], strlen(nvar) + 32, "L%d=%s", count++, nvar); } } set_env(envc, envp); diff --git a/unix/mblogin.c b/unix/mblogin.c index c69fffd1..3e21a3a6 100644 --- a/unix/mblogin.c +++ b/unix/mblogin.c @@ -501,7 +501,7 @@ int main(int argc, char **argv) /* get the mbse environment */ pw = getpwnam("mbse"); addenv("MBSE_ROOT", pw->pw_dir); - sprintf(userfile, "%s/etc/users.data", pw->pw_dir); + snprintf(userfile, PATH_MAX, "%s/etc/users.data", pw->pw_dir); check_nologin(); diff --git a/unix/mbpasswd.c b/unix/mbpasswd.c index 222c2144..caf6a71a 100644 --- a/unix/mbpasswd.c +++ b/unix/mbpasswd.c @@ -5,7 +5,7 @@ * Shadow Suite (c) ......: Julianne Frances Haugh * ***************************************************************************** - * Copyright (C) 1997-2003 + * Copyright (C) 1997-2005 * * Michiel Broek FIDO: 2:280/2802 * Beekmansbos 10 @@ -166,7 +166,7 @@ int execute(char **args, char *in, char *out, char *err) for (i = 0; i < 16; i++) { if (args[i]) - sprintf(buf, "%s %s", buf, args[i]); + snprintf(buf, PATH_MAX, "%s %s", buf, args[i]); else break; } @@ -611,7 +611,7 @@ static int new_password(const struct passwd *pw, char *newpasswd) int HistUpdate P_((const char *, const char *)); #endif - sprintf(pass, "%s", newpasswd); + snprintf(pass, 200, "%s", newpasswd); /* * Encrypt the password, then wipe the cleartext password. @@ -888,7 +888,7 @@ int main(int argc, char *argv[]) * Find out the name of our parent. */ ppid = getppid(); - sprintf(temp, "/proc/%d/cmdline", ppid); + snprintf(temp, PATH_MAX, "/proc/%d/cmdline", ppid); if ((fp = fopen(temp, "r")) == NULL) { fprintf(stderr, "mbpasswd: can't read %s\n", temp); syslog(LOG_ERR, "mbpasswd: can't read %s", temp); @@ -1055,7 +1055,7 @@ int main(int argc, char *argv[]) fflush(stdin); memset(args, 0, sizeof(args)); - sprintf(temp, "%s/vpasswd", (char *)_VPOPMAIL_PATH); + snprintf(temp, PATH_MAX, "%s/vpasswd", (char *)_VPOPMAIL_PATH); args[0] = temp; args[1] = argv[1]; args[2] = argv[2]; diff --git a/unix/mbuseradd.c b/unix/mbuseradd.c index 5f520c9e..0049d599 100644 --- a/unix/mbuseradd.c +++ b/unix/mbuseradd.c @@ -91,7 +91,7 @@ int execute(char **args, char *in, char *out, char *err) memset(&buf, 0, sizeof(buf)); for (i = 0; i < 16; i++) { if (args[i]) - sprintf(buf, "%s %s", buf, args[i]); + snprintf(buf, PATH_MAX, "%s %s", buf, args[i]); else break; } @@ -222,7 +222,7 @@ int main(int argc, char *argv[]) */ temp = calloc(PATH_MAX, sizeof(char)); ppid = getppid(); - sprintf(temp, "/proc/%d/cmdline", ppid); + snprintf(temp, PATH_MAX, "/proc/%d/cmdline", ppid); if ((fp = fopen(temp, "r")) == NULL) { fprintf(stderr, "mbuseradd: can't read %s\n", temp); exit(1); @@ -289,8 +289,8 @@ int main(int argc, char *argv[]) #error "Don't know how to add a user on this OS" #endif - sprintf(shell, "%s/bin/mbsebbs", getenv("MBSE_ROOT")); - sprintf(homedir, "%s/%s", argv[4], argv[2]); + snprintf(shell, PATH_MAX, "%s/bin/mbsebbs", getenv("MBSE_ROOT")); + snprintf(homedir, PATH_MAX, "%s/%s", argv[4], argv[2]); #if defined(__linux__) args[1] = (char *)"-c"; @@ -390,17 +390,17 @@ int main(int argc, char *argv[]) /* * Create Maildir and subdirs for Qmail. */ - sprintf(temp, "%s/%s/Maildir", argv[4], argv[2]); + snprintf(temp, PATH_MAX, "%s/%s/Maildir", argv[4], argv[2]); makedir(temp, 0700, pwuser->pw_uid, pwent->pw_gid); - sprintf(temp, "%s/%s/Maildir/cur", argv[4], argv[2]); + snprintf(temp, PATH_MAX, "%s/%s/Maildir/cur", argv[4], argv[2]); makedir(temp, 0700, pwuser->pw_uid, pwent->pw_gid); - sprintf(temp, "%s/%s/Maildir/new", argv[4], argv[2]); + snprintf(temp, PATH_MAX, "%s/%s/Maildir/new", argv[4], argv[2]); makedir(temp, 0700, pwuser->pw_uid, pwent->pw_gid); - sprintf(temp, "%s/%s/Maildir/tmp", argv[4], argv[2]); + snprintf(temp, PATH_MAX, "%s/%s/Maildir/tmp", argv[4], argv[2]); makedir(temp, 0700, pwuser->pw_uid, pwent->pw_gid); #ifdef _VPOPMAIL_PATH - sprintf(temp, "%s/vadduser", _VPOPMAIL_PATH); + snprintf(temp, PATH_MAX, "%s/vadduser", _VPOPMAIL_PATH); args[0] = temp; args[1] = argv[2]; args[2] = argv[2];