Skip to content

Commit

Permalink
util.c: add and use ascii-only char functions
Browse files Browse the repository at this point in the history
The "invalid_name" function claims to "allow strict ASCII letters and
numbers".

However, it uses isalnum(3) and isdigit(3), which may take the current
locale into account and thus return 1 for non-ASCII characters.

So add the following functions:

* ascii_isalnum
* ascii_isalpha
* ascii_isdigit
* ascii_islower
* ascii_isupper
* ascii_isxdigit

And use the applicable ones in "invalid_name" so that it actually uses
strictly ASCII in its comparisons.

Added on commit b4ffaa2 ("merges; more on cleaning up esc chars",
2023-02-14).

Relates to #5578.

Kind of relates to #5708.
  • Loading branch information
kmk3 committed Mar 21, 2023
1 parent 3e79412 commit f95bbb6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/firejail/firejail.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ int has_handler(pid_t pid, int signal);
void enter_network_namespace(pid_t pid);
int read_pid(const char *name, pid_t *pid);
pid_t require_pid(const char *name);
int ascii_isalnum(unsigned char c);
int ascii_isalpha(unsigned char c);
int ascii_isdigit(unsigned char c);
int ascii_islower(unsigned char c);
int ascii_isupper(unsigned char c);
int ascii_isxdigit(unsigned char c);
int invalid_name(const char *name);
void check_homedir(const char *dir);

Expand Down
31 changes: 29 additions & 2 deletions src/firejail/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1448,15 +1448,42 @@ static int has_link(const char *dir) {
return 0;
}

int ascii_isalnum(unsigned char c) {
return (ascii_isalpha(c) || ascii_isdigit(c));
}

int ascii_isalpha(unsigned char c) {
return (ascii_islower(c) || ascii_isupper(c));
}

int ascii_isdigit(unsigned char c) {
return (c >= '0' && c <= '9');
}

int ascii_islower(unsigned char c) {
return (c >= 'a' && c <= 'z');
}

int ascii_isupper(unsigned char c) {
return (c >= 'A' && c <= 'Z');
}

int ascii_isxdigit(unsigned char c) {
int ret = (ascii_isdigit(c) ||
(c >= 'a' && c <= 'f') ||
(c >= 'A' && c <= 'F'));
return ret;
}

// allow strict ASCII letters and numbers; names with only numbers are rejected; spaces are rejected
int invalid_name(const char *name) {
const char *c = name;

int only_numbers = 1;
while (*c) {
if (!isalnum(*c))
if (!ascii_isalnum(*c))
return 1;
if (!isdigit(*c))
if (!ascii_isdigit(*c))
only_numbers = 0;
++c;
}
Expand Down

0 comments on commit f95bbb6

Please sign in to comment.