X Tutup
Skip to content

Commit b508460

Browse files
committed
exec-util,conf-files: skip non-executable files in execute_directories()
Fixes: systemd#6787
1 parent 586377f commit b508460

File tree

22 files changed

+68
-35
lines changed

22 files changed

+68
-35
lines changed

src/basic/conf-files.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232
#include "macro.h"
3333
#include "missing.h"
3434
#include "path-util.h"
35+
#include "stat-util.h"
3536
#include "string-util.h"
3637
#include "strv.h"
3738
#include "util.h"
3839

39-
static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
40+
static int files_add(Hashmap *h, const char *suffix, const char *root, unsigned flags, const char *path) {
4041
_cleanup_closedir_ DIR *dir = NULL;
4142
const char *dirpath;
4243
struct dirent *de;
@@ -59,6 +60,31 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
5960
if (!dirent_is_file_with_suffix(de, suffix))
6061
continue;
6162

63+
if (flags & CONF_FILES_EXECUTABLE) {
64+
struct stat st;
65+
66+
/* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
67+
* here, as we care about whether the file is marked executable at all, and not whether it is
68+
* executable for us, because if such errors are stuff we should log about. */
69+
70+
if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
71+
log_debug_errno(errno, "Failed to stat %s/%s, ignoring: %m", dirpath, de->d_name);
72+
continue;
73+
}
74+
75+
/* We only want executable regular files (or symlinks to them), or symlinks to /dev/null */
76+
if (S_ISREG(st.st_mode)) {
77+
if ((st.st_mode & 0111) == 0) { /* not executable */
78+
log_debug("Ignoring %s/%s, as it is not marked executable.", dirpath, de->d_name);
79+
continue;
80+
}
81+
82+
} else if (!null_or_empty(&st)) { /* /dev/null? */
83+
log_debug("Ignoring %s/%s, as it is not a regular file (or symlink to /dev/null).", dirpath, de->d_name);
84+
continue;
85+
}
86+
}
87+
6288
p = strjoin(dirpath, "/", de->d_name);
6389
if (!p)
6490
return -ENOMEM;
@@ -87,7 +113,7 @@ static int base_cmp(const void *a, const void *b) {
87113
return strcmp(basename(s1), basename(s2));
88114
}
89115

90-
static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
116+
static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
91117
_cleanup_hashmap_free_ Hashmap *fh = NULL;
92118
char **files, **p;
93119
int r;
@@ -103,7 +129,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
103129
return -ENOMEM;
104130

105131
STRV_FOREACH(p, dirs) {
106-
r = files_add(fh, root, *p, suffix);
132+
r = files_add(fh, suffix, root, flags, *p);
107133
if (r == -ENOMEM)
108134
return r;
109135
if (r < 0)
@@ -120,7 +146,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
120146
return 0;
121147
}
122148

123-
int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char* const* dirs) {
149+
int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
124150
_cleanup_strv_free_ char **copy = NULL;
125151

126152
assert(strv);
@@ -129,10 +155,10 @@ int conf_files_list_strv(char ***strv, const char *suffix, const char *root, con
129155
if (!copy)
130156
return -ENOMEM;
131157

132-
return conf_files_list_strv_internal(strv, suffix, root, copy);
158+
return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
133159
}
134160

135-
int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...) {
161+
int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
136162
_cleanup_strv_free_ char **dirs = NULL;
137163
va_list ap;
138164

@@ -145,10 +171,10 @@ int conf_files_list(char ***strv, const char *suffix, const char *root, const ch
145171
if (!dirs)
146172
return -ENOMEM;
147173

148-
return conf_files_list_strv_internal(strv, suffix, root, dirs);
174+
return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
149175
}
150176

151-
int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *d) {
177+
int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *d) {
152178
_cleanup_strv_free_ char **dirs = NULL;
153179

154180
assert(strv);
@@ -157,5 +183,5 @@ int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, c
157183
if (!dirs)
158184
return -ENOMEM;
159185

160-
return conf_files_list_strv_internal(strv, suffix, root, dirs);
186+
return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
161187
}

src/basic/conf-files.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
along with systemd; If not, see <http://www.gnu.org/licenses/>.
2121
***/
2222

23-
int conf_files_list(char ***ret, const char *suffix, const char *root, const char *dir, ...);
24-
int conf_files_list_strv(char ***ret, const char *suffix, const char *root, const char* const* dirs);
25-
int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, const char *dirs);
23+
enum {
24+
CONF_FILES_EXECUTABLE = 1,
25+
};
26+
27+
int conf_files_list(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dir, ...);
28+
int conf_files_list_strv(char ***ret, const char *suffix, const char *root, unsigned flags, const char* const* dirs);
29+
int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dirs);

src/basic/exec-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static int do_execute(
111111

112112
assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
113113

114-
r = conf_files_list_strv(&paths, NULL, NULL, (const char* const*) directories);
114+
r = conf_files_list_strv(&paths, NULL, NULL, CONF_FILES_EXECUTABLE, (const char* const*) directories);
115115
if (r < 0)
116116
return r;
117117

src/binfmt/binfmt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ int main(int argc, char *argv[]) {
182182
_cleanup_strv_free_ char **files = NULL;
183183
char **f;
184184

185-
r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
185+
r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
186186
if (r < 0) {
187187
log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
188188
goto finish;

src/environment-d-generator/environment-d-generator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static int load_and_print(void) {
5858
if (r < 0)
5959
return r;
6060

61-
r = conf_files_list_strv(&files, ".conf", NULL, (const char **) dirs);
61+
r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char **) dirs);
6262
if (r < 0)
6363
return r;
6464

src/hwdb/hwdb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ static int hwdb_update(int argc, char *argv[], void *userdata) {
653653

654654
trie->nodes_count++;
655655

656-
r = conf_files_list_strv(&files, ".hwdb", arg_root, conf_file_dirs);
656+
r = conf_files_list_strv(&files, ".hwdb", arg_root, 0, conf_file_dirs);
657657
if (r < 0)
658658
return log_error_errno(r, "Failed to enumerate hwdb files: %m");
659659

src/journal/catalog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
479479
goto finish;
480480
}
481481

482-
r = conf_files_list_strv(&files, ".catalog", root, dirs);
482+
r = conf_files_list_strv(&files, ".catalog", root, 0, dirs);
483483
if (r < 0) {
484484
log_error_errno(r, "Failed to get catalog files: %m");
485485
goto finish;

src/modules-load/modules-load.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ int main(int argc, char *argv[]) {
264264
r = k;
265265
}
266266

267-
k = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
267+
k = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
268268
if (k < 0) {
269269
log_error_errno(k, "Failed to enumerate modules-load.d files: %m");
270270
if (r == 0)

src/network/netdev/netdev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ int netdev_load(Manager *manager) {
766766
while ((netdev = hashmap_first(manager->netdevs)))
767767
netdev_unref(netdev);
768768

769-
r = conf_files_list_strv(&files, ".netdev", NULL, network_dirs);
769+
r = conf_files_list_strv(&files, ".netdev", NULL, 0, network_dirs);
770770
if (r < 0)
771771
return log_error_errno(r, "Failed to enumerate netdev files: %m");
772772

src/network/networkd-network.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ int network_load(Manager *manager) {
322322
while ((network = manager->networks))
323323
network_free(network);
324324

325-
r = conf_files_list_strv(&files, ".network", NULL, network_dirs);
325+
r = conf_files_list_strv(&files, ".network", NULL, 0, network_dirs);
326326
if (r < 0)
327327
return log_error_errno(r, "Failed to enumerate network files: %m");
328328

0 commit comments

Comments
 (0)
X Tutup