Andrew Geissler | 517393d | 2023-01-13 08:55:19 -0600 | [diff] [blame^] | 1 | From b70e5bf5bfa5fa2c2fffe08bcf300da1d3583602 Mon Sep 17 00:00:00 2001 |
| 2 | From: Lars Ellenberg <lars.ellenberg@linbit.com> |
| 3 | Date: Wed, 9 Nov 2022 11:01:54 +0100 |
| 4 | Subject: [PATCH 2/2] drbdadm: drop use of GLOB_MAGCHAR, use strchr heuristic only |
| 5 | |
| 6 | Fixup for |
| 7 | 2022-09-05 4a1b5900 drbdadm: allow files from an expanded include glob to vanish |
| 8 | |
| 9 | When using the `include` statement, if the glob did not match any file, |
| 10 | there is nothing to do, silently ignore. Unless it was no glob, but a literal, |
| 11 | which we would expect to exist. |
| 12 | |
| 13 | Also, there is a race between expanding a glob and accessing the file. |
| 14 | That also should not happen for literals, though. |
| 15 | |
| 16 | Since we still had the heuristic anyways, because apparently |GLOB_MAGCHAR |
| 17 | does not happen for GLOB_NOMATCH returns, and there exist non-GNU libc that |
| 18 | don't (and likely won't) implement that extension, just forget about |
| 19 | (gl_flags & GLOB_MAGCHAR) but use the incomplete strchr heuristic only. |
| 20 | |
| 21 | Sourced From Alpine: https://git.alpinelinux.org/aports/tree/main/drbd-utils/drop_use_of_GLOB_MAGCHAR.patch |
| 22 | |
| 23 | Upstream-Status: Pending |
| 24 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 25 | --- |
| 26 | user/v9/drbdadm_parser.c | 35 ++++++++++++++++++++--------------- |
| 27 | 1 file changed, 20 insertions(+), 15 deletions(-) |
| 28 | |
| 29 | diff --git a/user/v9/drbdadm_parser.c b/user/v9/drbdadm_parser.c |
| 30 | index b2f6ed8a..9a0a775d 100644 |
| 31 | --- a/user/v9/drbdadm_parser.c |
| 32 | +++ b/user/v9/drbdadm_parser.c |
| 33 | @@ -1947,14 +1947,29 @@ void include_stmt(char *str) |
| 34 | size_t i; |
| 35 | int r; |
| 36 | |
| 37 | - cwd = pushd_to_current_config_file_unless_stdin(); |
| 38 | - |
| 39 | - /* """ |
| 40 | + /* |
| 41 | + * If the glob did not match any file, |
| 42 | + * there is nothing to do, silently ignore. |
| 43 | + * Unless it was no glob, but a literal, |
| 44 | + * which we would expect to exist. |
| 45 | + * |
| 46 | + * """ |
| 47 | * As a GNU extension, pglob->gl_flags is set to the |
| 48 | * flags specified, ored with GLOB_MAGCHAR if any |
| 49 | * metacharacters were found. |
| 50 | * """ |
| 51 | + * |
| 52 | + * But apparently |GLOB_MAGCHAR does not happen for GLOB_NOMATCH returns, |
| 53 | + * at least not consistently :-( |
| 54 | + * Also, there exist non-GNU libc |
| 55 | + * So we have this incomplete strchr heuristic anyways. |
| 56 | */ |
| 57 | + bool contains_glob_magic_char = |
| 58 | + strchr(str, '*') || |
| 59 | + strchr(str, '?') || |
| 60 | + strchr(str, '['); |
| 61 | + |
| 62 | + cwd = pushd_to_current_config_file_unless_stdin(); |
| 63 | r = glob(str, 0, NULL, &glob_buf); |
| 64 | if (r == 0) { |
| 65 | for (i=0; i<glob_buf.gl_pathc; i++) { |
| 66 | @@ -1965,7 +1980,7 @@ void include_stmt(char *str) |
| 67 | if (f) { |
| 68 | include_file(f, strdup(glob_buf.gl_pathv[i])); |
| 69 | fclose(f); |
| 70 | - } else if (errno == ENOENT && glob_buf.gl_flags & GLOB_MAGCHAR) { |
| 71 | + } else if (errno == ENOENT && contains_glob_magic_char) { |
| 72 | /* Noisily ignore race between glob expansion |
| 73 | * and actual open. */ |
| 74 | err("%s:%d: include file vanished after glob expansion '%s'.\n", |
| 75 | @@ -1979,17 +1994,7 @@ void include_stmt(char *str) |
| 76 | } |
| 77 | globfree(&glob_buf); |
| 78 | } else if (r == GLOB_NOMATCH) { |
| 79 | - /* |
| 80 | - * If the glob did not match any file, |
| 81 | - * there is nothing to do, silently ignore. |
| 82 | - * Unless it was no glob, but a literal, |
| 83 | - * which we would expect to exist. |
| 84 | - * Apparently |GLOB_MAGCHAR does not happen for GLOB_NOMATCH returns, |
| 85 | - * at least not consistently :-( |
| 86 | - * So we have this strchr heuristic anyways. |
| 87 | - */ |
| 88 | - /* if (!(glob_buf.gl_flags & GLOB_MAGCHAR)) { */ |
| 89 | - if (!strchr(str, '?') && !strchr(str, '*') && !strchr(str, '[')) { |
| 90 | + if (!contains_glob_magic_char) { |
| 91 | err("%s:%d: Failed to open include file '%s'.\n", |
| 92 | config_save, line, str); |
| 93 | config_valid = 0; |
| 94 | -- |
| 95 | 2.39.0 |
| 96 | |