-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libnss_tcb: Disallow potentially-malicious user names in getspnam(3).
Signed-off-by: Björn Esser <[email protected]>
- Loading branch information
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,31 @@ int _nss_tcb_endspent(void) | |
return 1; | ||
} | ||
|
||
/* IEEE Std 1003.1-2001 allows only the following characters to appear | ||
in group- and usernames: letters, digits, underscores, periods, | ||
<at>-signs (@), and dashes. The name may not start with a dash or @. | ||
The "$" sign is allowed at the end of usernames to allow typical | ||
Samba machine accounts. Regex: ^[a-z_.][[email protected]]*[$]$ */ | ||
static int | ||
is_valid_username (const char *un) | ||
{ | ||
if (!un || !strlen(un) || un[0] == '-' || un[0] == '@' || | ||
/* dirsep || curdir || parentdir */ | ||
strchr(un, '/') || !strcmp(un, ".") || !strcmp(un, "..")) | ||
return 0; | ||
|
||
int retval = 1; | ||
size_t unlen = strlen(un); | ||
|
||
for (size_t i = 0; !retval && i < unlen; i++) { | ||
char c = un[i]; | ||
retval = ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || | ||
(c >= '0' && c <= '9') || c == '-' || c == '.' || | ||
c == '@' || c == '_' || (i == unlen - 1 && c == '$')); | ||
} | ||
return retval; | ||
} | ||
|
||
static FILE *tcb_safe_open(const char *file, const char *name) | ||
{ | ||
gid_t grplist[TCB_NGROUPS]; | ||
|
@@ -64,6 +89,10 @@ int _nss_tcb_getspnam_r(const char *name, struct spwd *__result_buf, | |
char *file; | ||
int retval, saved_errno; | ||
|
||
/* Disallow potentially-malicious user names */ | ||
if (!is_valid_username(name)) | ||
return NSS_STATUS_NOTFOUND; | ||
|
||
if (asprintf(&file, TCB_FMT, name) < 0) | ||
return NSS_STATUS_TRYAGAIN; | ||
f = tcb_safe_open(file, name); | ||
|