Skip to content

Commit

Permalink
src/: Use getl() instead of atoi(3)
Browse files Browse the repository at this point in the history
atoi(3) easily triggers Undefined Behavior.  Replace it by getl(), which
is safe from that, and adds type safety too.

Signed-off-by: Alejandro Colomar <alx@kernel.org>
  • Loading branch information
alejandro-colomar committed Jan 8, 2024
1 parent e215388 commit 9302ca7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
13 changes: 9 additions & 4 deletions src/free_subid_range.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
/* SPDX-License-Identifier: BSD-3-Clause */


#include <stdio.h>
#include <unistd.h>

#include "atoi/getlong.h"
#include "subid.h"
#include "stdlib.h"
#include "prototypes.h"
#include "shadowlog.h"


/* Test program for the subid freeing routine */

const char *Prog;
Expand All @@ -20,8 +24,9 @@ static void usage(void)

int main(int argc, char *argv[])
{
int c;
bool ok;
int c;
bool ok;
long l;
struct subordinate_range range;
bool group = false; // get subuids by default

Expand All @@ -39,8 +44,8 @@ int main(int argc, char *argv[])
if (argc < 3)
usage();
range.owner = argv[0];
range.start = atoi(argv[1]);
range.count = atoi(argv[2]);
getl(argv[1], &range.start);
getl(argv[2], &range.count);
if (group)
ok = subid_ungrant_gid_range(&range);
else
Expand Down
22 changes: 15 additions & 7 deletions src/get_subid_owners.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/* SPDX-License-Identifier: BSD-3-Clause */


#include <stdio.h>

#include "atoi/getlong.h"
#include "subid.h"
#include "stdlib.h"
#include "prototypes.h"
#include "shadowlog.h"


const char *Prog;

static void usage(void)
Expand All @@ -18,21 +22,25 @@ static void usage(void)

int main(int argc, char *argv[])
{
int i, n;
uid_t *uids;
int i, n;
long l;
uid_t *uids;

Prog = Basename (argv[0]);
log_set_progname(Prog);
log_set_logfd(stderr);
if (argc < 2) {
usage();
}
if (argc == 3 && strcmp(argv[1], "-g") == 0)
n = subid_get_gid_owners(atoi(argv[2]), &uids);
else if (argc == 2 && strcmp(argv[1], "-h") == 0)
if (argc == 3 && strcmp(argv[1], "-g") == 0) {
getl(argv[2], &l);
n = subid_get_gid_owners(l, &uids);
} else if (argc == 2 && strcmp(argv[1], "-h") == 0) {
usage();
else
n = subid_get_uid_owners(atoi(argv[1]), &uids);
} else {
getl(argv[1], &l);
n = subid_get_uid_owners(l, &uids);
}
if (n < 0) {
fprintf(stderr, "No owners found\n");
exit(1);
Expand Down
5 changes: 4 additions & 1 deletion src/new_subid_range.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

#include <stdio.h>
#include <unistd.h>

#include "atoi/getlong.h"
#include "subid.h"
#include "stdlib.h"
#include "prototypes.h"
#include "shadowlog.h"


/* Test program for the subid creation routine */

const char *Prog;
Expand Down Expand Up @@ -46,7 +49,7 @@ int main(int argc, char *argv[])
range.start = 0;
range.count = 65536;
if (argc > 1)
range.count = atoi(argv[1]);
getl(argv[1], &range.count);
if (group)
ok = subid_grant_gid_range(&range, !makenew);
else
Expand Down

0 comments on commit 9302ca7

Please sign in to comment.