]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/binfmt/binfmt.c
binfmt: validate rule file name before using it
[thirdparty/systemd.git] / src / binfmt / binfmt.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <getopt.h>
5 #include <limits.h>
6 #include <stdbool.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "alloc-util.h"
12 #include "conf-files.h"
13 #include "def.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "log.h"
17 #include "pager.h"
18 #include "path-util.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "terminal-util.h"
22 #include "util.h"
23
24 static bool arg_cat_config = false;
25 static bool arg_no_pager = false;
26
27 static int delete_rule(const char *rule) {
28 _cleanup_free_ char *x = NULL, *fn = NULL;
29 char *e;
30
31 assert(rule);
32 assert(rule[0]);
33
34 x = strdup(rule);
35 if (!x)
36 return log_oom();
37
38 e = strchrnul(x+1, x[0]);
39 *e = 0;
40
41 if (!filename_is_valid(x + 1)) {
42 log_error("Rule file name '%s' is not valid, refusing.", x+1);
43 return -EINVAL;
44 }
45
46 fn = strappend("/proc/sys/fs/binfmt_misc/", x+1);
47 if (!fn)
48 return log_oom();
49
50 return write_string_file(fn, "-1", 0);
51 }
52
53 static int apply_rule(const char *rule) {
54 int r;
55
56 (void) delete_rule(rule);
57
58 r = write_string_file("/proc/sys/fs/binfmt_misc/register", rule, 0);
59 if (r < 0)
60 return log_error_errno(r, "Failed to add binary format: %m");
61
62 return 0;
63 }
64
65 static int apply_file(const char *path, bool ignore_enoent) {
66 _cleanup_fclose_ FILE *f = NULL;
67 int r;
68
69 assert(path);
70
71 r = search_and_fopen(path, "re", NULL, (const char**) CONF_PATHS_STRV("binfmt.d"), &f);
72 if (r < 0) {
73 if (ignore_enoent && r == -ENOENT)
74 return 0;
75
76 return log_error_errno(r, "Failed to open file '%s', ignoring: %m", path);
77 }
78
79 log_debug("apply: %s", path);
80 for (;;) {
81 char l[LINE_MAX], *p;
82 int k;
83
84 if (!fgets(l, sizeof(l), f)) {
85 if (feof(f))
86 break;
87
88 return log_error_errno(errno, "Failed to read file '%s', ignoring: %m", path);
89 }
90
91 p = strstrip(l);
92 if (!*p)
93 continue;
94 if (strchr(COMMENTS "\n", *p))
95 continue;
96
97 k = apply_rule(p);
98 if (k < 0 && r == 0)
99 r = k;
100 }
101
102 return r;
103 }
104
105 static int help(void) {
106 _cleanup_free_ char *link = NULL;
107 int r;
108
109 r = terminal_urlify_man("systemd-binfmt.service", "8", &link);
110 if (r < 0)
111 return log_oom();
112
113 printf("%s [OPTIONS...] [CONFIGURATION FILE...]\n\n"
114 "Registers binary formats with the kernel.\n\n"
115 " -h --help Show this help\n"
116 " --version Show package version\n"
117 " --cat-config Show configuration files\n"
118 " --no-pager Do not pipe output into a pager\n"
119 "\nSee the %s for details.\n"
120 , program_invocation_short_name
121 , link
122 );
123
124 return 0;
125 }
126
127 static int parse_argv(int argc, char *argv[]) {
128
129 enum {
130 ARG_VERSION = 0x100,
131 ARG_CAT_CONFIG,
132 ARG_NO_PAGER,
133 };
134
135 static const struct option options[] = {
136 { "help", no_argument, NULL, 'h' },
137 { "version", no_argument, NULL, ARG_VERSION },
138 { "cat-config", no_argument, NULL, ARG_CAT_CONFIG },
139 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
140 {}
141 };
142
143 int c;
144
145 assert(argc >= 0);
146 assert(argv);
147
148 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
149
150 switch (c) {
151
152 case 'h':
153 return help();
154
155 case ARG_VERSION:
156 return version();
157
158 case ARG_CAT_CONFIG:
159 arg_cat_config = true;
160 break;
161
162 case ARG_NO_PAGER:
163 arg_no_pager = true;
164 break;
165
166 case '?':
167 return -EINVAL;
168
169 default:
170 assert_not_reached("Unhandled option");
171 }
172
173 if (arg_cat_config && argc > optind) {
174 log_error("Positional arguments are not allowed with --cat-config");
175 return -EINVAL;
176 }
177
178 return 1;
179 }
180
181 int main(int argc, char *argv[]) {
182 int r, k;
183
184 r = parse_argv(argc, argv);
185 if (r <= 0)
186 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
187
188 log_set_target(LOG_TARGET_AUTO);
189 log_parse_environment();
190 log_open();
191
192 umask(0022);
193
194 r = 0;
195
196 if (argc > optind) {
197 int i;
198
199 for (i = optind; i < argc; i++) {
200 k = apply_file(argv[i], false);
201 if (k < 0 && r == 0)
202 r = k;
203 }
204 } else {
205 _cleanup_strv_free_ char **files = NULL;
206 char **f;
207
208 r = conf_files_list_strv(&files, ".conf", NULL, 0, (const char**) CONF_PATHS_STRV("binfmt.d"));
209 if (r < 0) {
210 log_error_errno(r, "Failed to enumerate binfmt.d files: %m");
211 goto finish;
212 }
213
214 if (arg_cat_config) {
215 (void) pager_open(arg_no_pager, false);
216
217 r = cat_files(NULL, files, 0);
218 goto finish;
219 }
220
221 /* Flush out all rules */
222 write_string_file("/proc/sys/fs/binfmt_misc/status", "-1", 0);
223
224 STRV_FOREACH(f, files) {
225 k = apply_file(*f, true);
226 if (k < 0 && r == 0)
227 r = k;
228 }
229 }
230
231 finish:
232 pager_close();
233
234 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
235 }