Skip to content

Commit

Permalink
chsh: limit acceptable shells to absolute paths
Browse files Browse the repository at this point in the history
If an entry in /etc/shells is not an absolute path (comments or
partial reads due to fgets), the line should not be considered as
a valid login shell.

In general all systems should have getusershells, but let's better
be safe than sorry.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
  • Loading branch information
stoeckmann authored and ikerexxe committed Nov 27, 2023
1 parent 721b909 commit 4b89ac4
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/chsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,17 @@ static bool shell_is_listed (const char *sh)
}
endusershell ();
#else
char buf[BUFSIZ];
char *buf = NULL;
FILE *fp;
size_t n = 0;

fp = fopen (SHELLS_FILE, "r");
if (NULL == fp) {
return false;
}

while (fgets (buf, sizeof (buf), fp) == buf) {
cp = strrchr (buf, '\n');
if (NULL != cp) {
*cp = '\0';
}

if (buf[0] == '#') {
while (getline (&buf, &n, fp) != -1) {
if (buf[0] != '/') {
continue;
}

Expand All @@ -227,6 +223,8 @@ static bool shell_is_listed (const char *sh)
break;
}
}

free(buf);
fclose (fp);
#endif
return found;
Expand Down

0 comments on commit 4b89ac4

Please sign in to comment.