Skip to content

src/newusers.c: A user/group name with a leading digit is valid#1475

Open
alejandro-colomar wants to merge 1 commit into
shadow-maint:masterfrom
alejandro-colomar:isdigit
Open

src/newusers.c: A user/group name with a leading digit is valid#1475
alejandro-colomar wants to merge 1 commit into
shadow-maint:masterfrom
alejandro-colomar:isdigit

Conversation

@alejandro-colomar

@alejandro-colomar alejandro-colomar commented Jan 3, 2026

Copy link
Copy Markdown
Collaborator

Only consider a string to be a UID/GID if it is all digits.

Closes: #1474


Cc: @uecker


Revisions:

v1b
  • Add reproducer to commit message.
$ git rd 
1:  f26af1fee ! 1:  497cbf1fe src/newusers.c: A user/group name with a leading digit is valid
    @@ Commit message
     
         Only consider a string to be a UID/GID if it is all digits.
     
    +    Here's a reproducer of the bug:
    +
    +            $ echo 'foo:p::1a::/tmp/nonexistent:/usr/bin/false' > x
    +            $ sudo newusers ./x
    +            newusers: invalid group ID '1a'
    +            newusers: line 1: can't create group
    +
    +    Where the expected behavior would be the same as for a group name that
    +    doesn't start with a digit:
    +
    +            $ echo 'foo:p::a1a::/tmp/nonexistent:/usr/bin/false' > x
    +            $ sudo newusers ./x
    +            $ tail -n1 /etc/group
    +            a1a:x:1004:
    +            $ tail -n1 /etc/passwd
    +            foo:x:1004:1004::/tmp/nonexistent:/usr/bin/false
    +
         Closes: <https://github.com/shadow-maint/shadow/issues/1474>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
v1c
  • Rebase
$ git rd 
1:  8d4aff7d = 1:  98677c4e src/newusers.c: A user/group name with a leading digit is valid
v1d
  • Rebase
$ git rd --creation-factor=99
1:  98677c4e39b2 ! 1:  8cbf06e0ebfb src/newusers.c: A user/group name with a leading digit is valid
    @@ Commit message
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## src/newusers.c ##
    -@@
    - #include "shadow/gshadow/sgrp.h"
    - #include "shadowlog.h"
    - #include "sssd.h"
    -+#include "string/ctype/strisascii/strisdigit.h"
    - #include "string/sprintf/snprintf.h"
    - #include "string/strcmp/streq.h"
    - #include "string/strdup/strdup.h"
     @@ src/newusers.c: static int add_group (const char *name, const char *gid, gid_t *ngid, uid_t uid)
                return 0;
        }
      
    --  if (isdigit (gid[0])) {
    -+  if (!streq(gid, "") && strisdigit(gid)) {
    +-  if (isdigit_c(gid[0])) {
    ++  if (!streq(gid, "") && strisdigit_c(gid)) {
                /*
                 * The GID is a number, which means either this is a brand
                 * new group, or an existing group.
    @@ src/newusers.c: static int add_group (const char *name, const char *gid, gid_t *
        /*
         * Now I have all of the fields required to create the new group.
         */
    --  if (!streq(gid, "") && (!isdigit(gid[0]))) {
    -+  if (!streq(gid, "") && !strisdigit(gid)) {
    +-  if (!streq(gid, "") && (!isdigit_c(gid[0]))) {
    ++  if (!streq(gid, "") && !strisdigit_c(gid)) {
                grent.gr_name = xstrdup (gid);
        } else {
                grent.gr_name = xstrdup (name);
    @@ src/newusers.c: static int get_user_id (const char *uid, uid_t *nuid) {
         * The first guess for the UID is either the numerical UID that the
         * caller provided, or the next available UID.
         */
    --  if (isdigit (uid[0])) {
    -+  if (!streq(uid, "") && strisdigit(uid)) {
    +-  if (isdigit_c(uid[0])) {
    ++  if (!streq(uid, "") && strisdigit_c(uid)) {
                if ((get_uid(uid, nuid) == -1) || (*nuid == (uid_t)-1)) {
                        fprintf (stderr,
                                 _("%s: invalid user ID '%s'\n"),

Comment thread src/newusers.c Outdated
Comment thread src/newusers.c Outdated
Only consider a string to be a UID/GID if it is all digits.

Here's a reproducer of the bug:

	$ echo 'foo:p::1a::/tmp/nonexistent:/usr/bin/false' > x
	$ sudo newusers ./x
	newusers: invalid group ID '1a'
	newusers: line 1: can't create group

Where the expected behavior would be the same as for a group name that
doesn't start with a digit:

	$ echo 'foo:p::a1a::/tmp/nonexistent:/usr/bin/false' > x
	$ sudo newusers ./x
	$ tail -n1 /etc/group
	a1a:x:1004:
	$ tail -n1 /etc/passwd
	foo:x:1004:1004::/tmp/nonexistent:/usr/bin/false

Closes: <shadow-maint#1474>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect use of isdigit(3) where strisdigit() is wanted

2 participants