Skip to content

Commit

Permalink
Hardcoding Prog to known value
Browse files Browse the repository at this point in the history
See #959. We now set Prog (program name) based on hardcoded value instead
of argv[0]. This is to help prevent escape sequence injection.
  • Loading branch information
skyler-ferrante authored and alejandro-colomar committed Mar 7, 2024
1 parent d138444 commit e6c2e43
Show file tree
Hide file tree
Showing 37 changed files with 85 additions and 171 deletions.
7 changes: 3 additions & 4 deletions src/chage.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
/*
* Global variables
*/
const char *Prog;
static const char Prog[] = "chage";

static bool
dflg = false, /* set last password change date */
Expand Down Expand Up @@ -517,7 +517,7 @@ static void check_perms (void)
exit (E_NOPERM);
}

retval = pam_start ("chage", pampw->pw_name, &conv, &pamh);
retval = pam_start (Prog, pampw->pw_name, &conv, &pamh);

if (PAM_SUCCESS == retval) {
retval = pam_authenticate (pamh, 0);
Expand Down Expand Up @@ -771,7 +771,6 @@ int main (int argc, char **argv)
/*
* Get the program name so that error messages can use it.
*/
Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

Expand All @@ -786,7 +785,7 @@ int main (int argc, char **argv)
#ifdef WITH_AUDIT
audit_help_open ();
#endif
OPENLOG ("chage");
OPENLOG (Prog);

ruid = getuid ();
rgid = getgid ();
Expand Down
3 changes: 1 addition & 2 deletions src/check_subid_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@
#include "idmapping.h"
#include "shadowlog.h"

const char *Prog;
static const char Prog[] = "check_subid_range";

int main(int argc, char **argv)
{
char *owner;
unsigned long start, count;
bool check_uids;
Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

Expand Down
15 changes: 5 additions & 10 deletions src/chfn.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
/*
* Global variables.
*/
const char *Prog;
static const char Prog[] = "chfn";
static char fullnm[BUFSIZ];
static char roomno[BUFSIZ];
static char workph[BUFSIZ];
Expand Down Expand Up @@ -365,7 +365,7 @@ static void check_perms (const struct passwd *pw)
* check if the change is allowed by SELinux policy.
*/
if ((pw->pw_uid != getuid ())
&& (check_selinux_permit ("chfn") != 0)) {
&& (check_selinux_permit (Prog) != 0)) {
fprintf (stderr, _("%s: Permission denied.\n"), Prog);
closelog ();
exit (E_NOPERM);
Expand All @@ -380,7 +380,7 @@ static void check_perms (const struct passwd *pw)
* --marekm
*/
if (!amroot && getdef_bool ("CHFN_AUTH")) {
passwd_check (pw->pw_name, pw->pw_passwd, "chfn");
passwd_check (pw->pw_name, pw->pw_passwd, Prog);
}

#else /* !USE_PAM */
Expand All @@ -392,7 +392,7 @@ static void check_perms (const struct passwd *pw)
exit (E_NOPERM);
}

retval = pam_start ("chfn", pampw->pw_name, &conv, &pamh);
retval = pam_start (Prog, pampw->pw_name, &conv, &pamh);

if (PAM_SUCCESS == retval) {
retval = pam_authenticate (pamh, 0);
Expand Down Expand Up @@ -620,11 +620,6 @@ int main (int argc, char **argv)
char *user;
const struct passwd *pw;

/*
* Get the program name. The program name is used as a
* prefix to most error messages.
*/
Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

Expand All @@ -641,7 +636,7 @@ int main (int argc, char **argv)
*/
amroot = (getuid () == 0);

OPENLOG ("chfn");
OPENLOG (Prog);

/* parse the command line options */
process_flags (argc, argv);
Expand Down
7 changes: 3 additions & 4 deletions src/chgpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/*
* Global variables
*/
const char *Prog;
static const char Prog[] = "chgpasswd";
static bool eflg = false;
static bool md5flg = false;
#if defined(USE_SHA_CRYPT) || defined(USE_BCRYPT) || defined(USE_YESCRYPT)
Expand Down Expand Up @@ -303,7 +303,7 @@ static void check_perms (void)
exit (1);
}

retval = pam_start ("chgpasswd", pampw->pw_name, &conv, &pamh);
retval = pam_start (Prog, pampw->pw_name, &conv, &pamh);

if (PAM_SUCCESS == retval) {
retval = pam_authenticate (pamh, 0);
Expand Down Expand Up @@ -423,7 +423,6 @@ int main (int argc, char **argv)
int errors = 0;
int line = 0;

Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

Expand All @@ -441,7 +440,7 @@ int main (int argc, char **argv)

process_flags (argc, argv);

OPENLOG ("chgpasswd");
OPENLOG (Prog);

check_perms ();

Expand Down
9 changes: 4 additions & 5 deletions src/chpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/*
* Global variables
*/
const char *Prog;
static const char Prog[] = "chpasswd";
static bool eflg = false;
static bool md5flg = false;
#if defined(USE_SHA_CRYPT) || defined(USE_BCRYPT) || defined(USE_YESCRYPT)
Expand Down Expand Up @@ -302,7 +302,7 @@ static void check_perms (void)
exit (1);
}

retval = pam_start ("chpasswd", pampw->pw_name, &conv, &pamh);
retval = pam_start (Prog, pampw->pw_name, &conv, &pamh);

if (PAM_SUCCESS == retval) {
retval = pam_authenticate (pamh, 0);
Expand Down Expand Up @@ -450,7 +450,6 @@ int main (int argc, char **argv)
int errors = 0;
int line = 0;

Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

Expand All @@ -476,7 +475,7 @@ int main (int argc, char **argv)
}
#endif /* USE_PAM */

OPENLOG ("chpasswd");
OPENLOG (Prog);

check_perms ();

Expand Down Expand Up @@ -546,7 +545,7 @@ int main (int argc, char **argv)

#ifdef USE_PAM
if (use_pam) {
if (do_pam_passwd_non_interactive ("chpasswd", name, newpwd) != 0) {
if (do_pam_passwd_non_interactive (Prog, name, newpwd) != 0) {
fprintf (stderr,
_("%s: (line %d, user %s) password not changed\n"),
Prog, line, name);
Expand Down
15 changes: 5 additions & 10 deletions src/chsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
/*
* Global variables
*/
const char *Prog; /* Program name */
static const char Prog[] = "chsh"; /* Program name */
static bool amroot; /* Real UID is root */
static char loginsh[BUFSIZ]; /* Name of new login shell */
/* command line options */
Expand Down Expand Up @@ -319,7 +319,7 @@ static void check_perms (const struct passwd *pw)
* check if the change is allowed by SELinux policy.
*/
if ((pw->pw_uid != getuid ())
&& (check_selinux_permit("chsh") != 0)) {
&& (check_selinux_permit(Prog) != 0)) {
SYSLOG ((LOG_WARN, "can't change shell for '%s'", pw->pw_name));
fprintf (stderr,
_("You may not change the shell for '%s'.\n"),
Expand All @@ -336,7 +336,7 @@ static void check_perms (const struct passwd *pw)
* chfn/chsh. --marekm
*/
if (!amroot && getdef_bool ("CHSH_AUTH")) {
passwd_check (pw->pw_name, pw->pw_passwd, "chsh");
passwd_check (pw->pw_name, pw->pw_passwd, Prog);
}

#else /* !USE_PAM */
Expand All @@ -348,7 +348,7 @@ static void check_perms (const struct passwd *pw)
exit (E_NOPERM);
}

retval = pam_start ("chsh", pampw->pw_name, &conv, &pamh);
retval = pam_start (Prog, pampw->pw_name, &conv, &pamh);

if (PAM_SUCCESS == retval) {
retval = pam_authenticate (pamh, 0);
Expand Down Expand Up @@ -473,11 +473,6 @@ int main (int argc, char **argv)

sanitize_env ();

/*
* Get the program name. The program name is used as a prefix to
* most error messages.
*/
Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

Expand All @@ -492,7 +487,7 @@ int main (int argc, char **argv)
*/
amroot = (getuid () == 0);

OPENLOG ("chsh");
OPENLOG (Prog);

/* parse the command line options */
process_flags (argc, argv);
Expand Down
5 changes: 2 additions & 3 deletions src/expiry.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "shadowlog.h"

/* Global variables */
const char *Prog;
static const char Prog[] = "expiry";
static bool cflg = false;

/* local function prototypes */
Expand Down Expand Up @@ -125,7 +125,6 @@ int main (int argc, char **argv)
struct passwd *pwd;
struct spwd *spwd;

Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

Expand All @@ -147,7 +146,7 @@ int main (int argc, char **argv)
(void) bindtextdomain (PACKAGE, LOCALEDIR);
(void) textdomain (PACKAGE);

OPENLOG ("expiry");
OPENLOG (Prog);

process_flags (argc, argv);

Expand Down
7 changes: 1 addition & 6 deletions src/faillog.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void reset (void);
/*
* Global variables
*/
const char *Prog; /* Program name */
static const char Prog[] = "faillog"; /* Program name */
static FILE *fail; /* failure file stream */
static time_t seconds; /* that number of days in seconds */
static unsigned long umin; /* if uflg and has_umin, only display users with uid >= umin */
Expand Down Expand Up @@ -510,11 +510,6 @@ int main (int argc, char **argv)
short fail_max = 0; // initialize to silence compiler warning
long days = 0;

/*
* Get the program name. The program name is used as a prefix to
* most error messages.
*/
Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

Expand Down
3 changes: 1 addition & 2 deletions src/free_subid_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/* Test program for the subid freeing routine */

const char *Prog;
static const char Prog[] = "free_subid_range";

static void usage(void)
{
Expand All @@ -25,7 +25,6 @@ int main(int argc, char *argv[])
struct subordinate_range range;
bool group = false; // get subuids by default

Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);
while ((c = getopt(argc, argv, "g")) != EOF) {
Expand Down
3 changes: 1 addition & 2 deletions src/get_subid_owners.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "prototypes.h"
#include "shadowlog.h"

const char *Prog;
static const char Prog[] = "get_subid_owners";

static void usage(void)
{
Expand All @@ -21,7 +21,6 @@ int main(int argc, char *argv[])
int i, n;
uid_t *uids;

Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);
if (argc < 2) {
Expand Down
3 changes: 1 addition & 2 deletions src/getsubids.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "prototypes.h"
#include "shadowlog.h"

const char *Prog;
static const char Prog[] = "getsubids";

static void usage(void)
{
Expand All @@ -23,7 +23,6 @@ int main(int argc, char *argv[])
struct subid_range *ranges;
const char *owner;

Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);
if (argc < 2)
Expand Down
5 changes: 2 additions & 3 deletions src/gpasswd.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* Global variables
*/
/* The name of this command, as it is invoked */
const char *Prog;
static const char Prog[] = "gpasswd";

#ifdef SHADOWGRP
/* Indicate if shadow groups are enabled on the system
Expand Down Expand Up @@ -942,11 +942,10 @@ int main (int argc, char **argv)
* with this command.
*/
bywho = getuid ();
Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);

OPENLOG ("gpasswd");
OPENLOG (Prog);
setbuf (stdout, NULL);
setbuf (stderr, NULL);

Expand Down

0 comments on commit e6c2e43

Please sign in to comment.