1
0
mirror of https://github.com/systemd/systemd synced 2025-10-03 18:54:45 +02:00

Compare commits

...

13 Commits

Author SHA1 Message Date
Luca Boccassi
1d2e9c48e5
Merge pull request #18930 from anitazha/oomdfixleak
oomd: fix memory leak
2021-03-09 11:37:10 +00:00
Anita Zhang
50c0578b61 oomd: wrap paths in oomd_insert_cgroup_context with empty_to_root 2021-03-09 09:23:22 +01:00
Yu Watanabe
2eaed57bd4
Merge pull request #18932 from poettering/filename-max
Drop use of FILENAME_MAX
2021-03-09 14:15:49 +09:00
Zbigniew Jędrzejewski-Szmek
03a81441b1 timedated: fix skipping of comments in config file
Reading file '/usr/lib/systemd/ntp-units.d/80-systemd-timesync.list'
Failed to add NTP service "# This file is part of systemd.", ignoring: Invalid argument
Failed to add NTP service "# See systemd-timedated.service(8) for more information.", ignoring: Invalid argument

:(
2021-03-09 14:04:21 +09:00
Anita Zhang
45da27fa05 oomd: move TAKE_PTR to end of oomd_insert_cgroup_context()
Fixes #18926
2021-03-08 14:37:15 -08:00
Anita Zhang
399d80ba8c oomd: add unit test to repro #18926 2021-03-08 14:36:24 -08:00
Lennart Poettering
698660620d test: output FILENAME_MAX vs. PATH_MAX sizes
Also, make sure our assumption that FILENAME_MAX == PATH_MAX holds.
2021-03-08 22:52:04 +01:00
Lennart Poettering
445714569d mountpoint-util: replace our last use of FILENAME_MAX by PATH_MAX 2021-03-08 22:47:55 +01:00
Lennart Poettering
db22003233 fs-util: replace use of FILENAME_MAX by PATH_MAX in readlinkat_malloc()
While we are at it, let's also add an overflow check and do other
modernizations.
2021-03-08 22:47:51 +01:00
Lennart Poettering
932401fd61 docs: reference NAME_MAX where we talk about filenames 2021-03-08 22:47:48 +01:00
Lennart Poettering
b775b1828d docs: document not to use FILENAME_MAX in our codebase
It's a weird thing. Let's explain why.
2021-03-08 22:47:44 +01:00
Lennart Poettering
f470d234d3 efi-loader: make efi_loader_entry_name_valid() check a bit stricter
Previously we'd just check if the ID was no-empty an no longer than
FILENAME_MAX. The latter was probably a mistake, given the comment next
to it. Instead of fixing that to check for NAME_MAX let's instead  just
switch over to filename_is_valid() which odes a similar check, plus a
some minor additional checks. After all we do want that valid EFI boot
menu entry ids are usable as filenames.
2021-03-08 22:47:41 +01:00
Lennart Poettering
8ca94009f8 basic: tighten two filename length checks
This fixes two checks where we compare string sizes when validating with
FILENAME_MAX. In both cases the check apparently wants to check if the
name fits in a filename, but that's not actually what FILENAME_MAX can
be used for, as it — in contrast to what the name suggests — actually
encodes the maximum length of a path.

In both cases the stricter change doesn't actually change much, but the
use of FILENAME_MAX is still misleading and typically wrong.
2021-03-08 22:47:14 +01:00
12 changed files with 44 additions and 26 deletions

View File

@ -587,6 +587,12 @@ layout: default
time you need that please immediately undefine `basename()`, and add a time you need that please immediately undefine `basename()`, and add a
comment about it, so that no code ever ends up using the POSIX version! comment about it, so that no code ever ends up using the POSIX version!
- Never use FILENAME_MAX. Use PATH_MAX instead (for checking maximum size of
paths) and NAME_MAX (for checking maximum size of filenames). FILENAME_MAX is
not POSIX, and is a confusingly named alias for PATH_MAX on Linux. Note the
NAME_MAX does not include space for a trailing NUL, but PATH_MAX does. UNIX
FTW!
## Committing to git ## Committing to git
- Commit message subject lines should be prefixed with an appropriate component - Commit message subject lines should be prefixed with an appropriate component

View File

@ -87,8 +87,8 @@ hyphen. A size limit is enforced: the minimum of `sysconf(_SC_LOGIN_NAME_MAX)`
(typically 256 on Linux; rationale: this is how POSIX suggests to detect the (typically 256 on Linux; rationale: this is how POSIX suggests to detect the
limit), `UT_NAMESIZE-1` (typically 31 on Linux; rationale: names longer than limit), `UT_NAMESIZE-1` (typically 31 on Linux; rationale: names longer than
this cannot correctly appear in `utmp`/`wtmp` and create ambiguity with login this cannot correctly appear in `utmp`/`wtmp` and create ambiguity with login
accounting) and `FILENAME_MAX` (4096 on Linux; rationale: user names typically accounting) and `NAME_MAX` (255 on Linux; rationale: user names typically
appear in directory names, i.e. the home directory), thus MIN(256, 31, 4096) = appear in directory names, i.e. the home directory), thus MIN(256, 31, 255) =
31. 31.
Note that these rules are both more strict and more relaxed than all of the Note that these rules are both more strict and more relaxed than all of the

View File

@ -1567,7 +1567,7 @@ bool cg_controller_is_valid(const char *p) {
if (!strchr(CONTROLLER_VALID, *t)) if (!strchr(CONTROLLER_VALID, *t))
return false; return false;
if (t - p > FILENAME_MAX) if (t - p > NAME_MAX)
return false; return false;
return true; return true;

View File

@ -135,34 +135,34 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char
} }
int readlinkat_malloc(int fd, const char *p, char **ret) { int readlinkat_malloc(int fd, const char *p, char **ret) {
size_t l = FILENAME_MAX+1; size_t l = PATH_MAX;
int r;
assert(p); assert(p);
assert(ret); assert(ret);
for (;;) { for (;;) {
char *c; _cleanup_free_ char *c = NULL;
ssize_t n; ssize_t n;
c = new(char, l); c = new(char, l+1);
if (!c) if (!c)
return -ENOMEM; return -ENOMEM;
n = readlinkat(fd, p, c, l-1); n = readlinkat(fd, p, c, l);
if (n < 0) { if (n < 0)
r = -errno; return -errno;
free(c);
return r;
}
if ((size_t) n < l-1) { if ((size_t) n < l) {
c[n] = 0; c[n] = 0;
*ret = c; *ret = TAKE_PTR(c);
return 0; return 0;
} }
free(c); if (l > (SSIZE_MAX-1)/2) /* readlinkat() returns an ssize_t, and we want an extra byte for a
* trailing NUL, hence do an overflow check relative to SSIZE_MAX-1
* here */
return -EFBIG;
l *= 2; l *= 2;
} }
} }

View File

@ -148,7 +148,7 @@ static bool filename_possibly_with_slash_suffix(const char *s) {
if (!slash) if (!slash)
return filename_is_valid(s); return filename_is_valid(s);
if (slash - s > FILENAME_MAX) /* We want to allocate on the stack below, hence do a size check first */ if (slash - s > PATH_MAX) /* We want to allocate on the stack below, hence do a size check first */
return false; return false;
if (slash[strspn(slash, "/")] != 0) /* Check that the suffix consist only of one or more slashes */ if (slash[strspn(slash, "/")] != 0) /* Check that the suffix consist only of one or more slashes */

View File

@ -836,7 +836,7 @@ bool valid_user_group_name(const char *u, ValidUserFlags flags) {
if (l > (size_t) sz) if (l > (size_t) sz)
return false; return false;
if (l > FILENAME_MAX) if (l > NAME_MAX) /* must fit in a filename */
return false; return false;
if (l > UT_NAMESIZE - 1) if (l > UT_NAMESIZE - 1)
return false; return false;

View File

@ -112,7 +112,7 @@ static int process_managed_oom_reply(
continue; continue;
} }
ret = oomd_insert_cgroup_context(NULL, monitor_hm, empty_to_root(reply.path)); ret = oomd_insert_cgroup_context(NULL, monitor_hm, reply.path);
if (ret == -ENOMEM) { if (ret == -ENOMEM) {
r = ret; r = ret;
goto finish; goto finish;

View File

@ -384,16 +384,20 @@ int oomd_system_context_acquire(const char *proc_swaps_path, OomdSystemContext *
int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path) { int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path) {
_cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *curr_ctx = NULL; _cleanup_(oomd_cgroup_context_freep) OomdCGroupContext *curr_ctx = NULL;
OomdCGroupContext *old_ctx, *ctx; OomdCGroupContext *old_ctx;
int r; int r;
assert(new_h); assert(new_h);
assert(path); assert(path);
path = empty_to_root(path);
r = oomd_cgroup_context_acquire(path, &curr_ctx); r = oomd_cgroup_context_acquire(path, &curr_ctx);
if (r < 0) if (r < 0)
return log_debug_errno(r, "Failed to get OomdCGroupContext for %s: %m", path); return log_debug_errno(r, "Failed to get OomdCGroupContext for %s: %m", path);
assert_se(streq(path, curr_ctx->path));
old_ctx = hashmap_get(old_h, path); old_ctx = hashmap_get(old_h, path);
if (old_ctx) { if (old_ctx) {
curr_ctx->last_pgscan = old_ctx->pgscan; curr_ctx->last_pgscan = old_ctx->pgscan;
@ -401,11 +405,11 @@ int oomd_insert_cgroup_context(Hashmap *old_h, Hashmap *new_h, const char *path)
curr_ctx->last_hit_mem_pressure_limit = old_ctx->last_hit_mem_pressure_limit; curr_ctx->last_hit_mem_pressure_limit = old_ctx->last_hit_mem_pressure_limit;
} }
ctx = TAKE_PTR(curr_ctx); r = hashmap_put(new_h, curr_ctx->path, curr_ctx);
r = hashmap_put(new_h, ctx->path, ctx);
if (r < 0) if (r < 0)
return r; return r;
TAKE_PTR(curr_ctx);
return 0; return 0;
} }

View File

@ -150,6 +150,7 @@ static void test_oomd_cgroup_context_acquire_and_insert(void) {
assert_se(oomd_insert_cgroup_context(NULL, h1, cgroup) == 0); assert_se(oomd_insert_cgroup_context(NULL, h1, cgroup) == 0);
c1 = hashmap_get(h1, cgroup); c1 = hashmap_get(h1, cgroup);
assert_se(c1); assert_se(c1);
assert_se(oomd_insert_cgroup_context(NULL, h1, cgroup) == -EEXIST);
/* make sure certain values from h1 get updated in h2 */ /* make sure certain values from h1 get updated in h2 */
c1->pgscan = 5555; c1->pgscan = 5555;

View File

@ -809,10 +809,8 @@ bool efi_has_tpm2(void) {
#endif #endif
bool efi_loader_entry_name_valid(const char *s) { bool efi_loader_entry_name_valid(const char *s) {
if (isempty(s))
return false;
if (strlen(s) > FILENAME_MAX) /* Make sure entry names fit in filenames */ if (!filename_is_valid(s)) /* Make sure entry names fit in filenames */
return false; return false;
return in_charset(s, ALPHANUMERICAL "+-_."); return in_charset(s, ALPHANUMERICAL "+-_.");

View File

@ -838,6 +838,15 @@ static void test_path_startswith_strv(void) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
test_setup_logging(LOG_DEBUG); test_setup_logging(LOG_DEBUG);
log_info("PATH_MAX=%zu\n"
"FILENAME_MAX=%zu\n"
"NAME_MAX=%zu",
(size_t) PATH_MAX,
(size_t) FILENAME_MAX,
(size_t) NAME_MAX);
assert_cc(FILENAME_MAX == PATH_MAX);
test_print_paths(); test_print_paths();
test_path(); test_path();
test_path_equal_root(); test_path_equal_root();

View File

@ -211,7 +211,7 @@ static int context_parse_ntp_services_from_disk(Context *c) {
break; break;
word = strstrip(line); word = strstrip(line);
if (isempty(word) || startswith("#", word)) if (isempty(word) || startswith(word, "#"))
continue; continue;
r = context_add_ntp_service(c, word, *f); r = context_add_ntp_service(c, word, *f);