Debian Codegraph @

src/basic/argv-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4 #include <stdlib.h>
5 #include <sys/mman.h>
6 #include <sys/prctl.h>
7
8 #include "argv-util.h"
9 #include "capability-util.h"
10 #include "errno-util.h"
11 #include "log.h"
12 #include "parse-util.h"
13 #include "path-util.h"
14 #include "process-util.h"
15 #include "string-util.h"
16 #include "strv.h"
17
18 int saved_argc = 0;
19 char **saved_argv = NULL;
20
21 void save_argc_argv(int argc, char **argv) {
22 /* Protect against CVE-2021-4034 style attacks */
23 assert_se(argc > 0);
24 assert_se(argv);
25 assert_se(argv[0]);
26
27 saved_argc = argc;
28 saved_argv = argv;
29 }
30
31 bool invoked_as(char *argv[], const char *token) {
32 const char *progname = secure_getenv("SYSTEMD_INVOKED_AS");
33
34 if (!progname && argv)
35 progname = argv[0];
36
37 if (isempty(progname) || isempty(token))
38 return false;
39
40 return strstr(last_path_component(progname), token);
41 }
42
43 bool invoked_by_systemd(void) {
44 static int cached = -1;
45 int r;
46
47 if (cached >= 0)
48 return cached;
49
50 /* If the process is directly executed by PID1 (e.g. ExecStart= or generator), systemd-importd,
51 * or systemd-homed, then $SYSTEMD_EXEC_PID= is set, and read the command line. */
52 const char *e = getenv("SYSTEMD_EXEC_PID");
53 if (!e)
54 return (cached = false);
55
56 if (streq(e, "*"))
57 /* For testing. */
58 return (cached = true);
59
60 pid_t p;
61 r = parse_pid(e, &p);
62 if (r < 0) {
63 /* We know that systemd sets the variable correctly. Something else must have set it. */
64 log_debug_errno(r, "Failed to parse \"SYSTEMD_EXEC_PID=%s\", ignoring: %m", e);
65 return (cached = false);
66 }
67
68 cached = getpid_cached() == p;
69 return cached;
70 }
71
72 bool argv_looks_like_help(int argc, char **argv) {
73 char **l;
74
75 /* Scans the command line for indications the user asks for help. This is supposed to be called by
76 * tools that do not implement getopt()-style command line parsing because they are not primarily
77 * user-facing. Detects four ways of asking for help:
78 *
79 * 1. Passing zero arguments
80 * 2. Passing "help" as first argument
81 * 3. Passing --help as any argument
82 * 4. Passing -h as any argument
83 */
84
85 if (argc <= 1)
86 return true;
87
88 if (streq_ptr(argv[1], "help"))
89 return true;
90
91 l = strv_skip(argv, 1);
92
93 return strv_contains(l, "--help") ||
94 strv_contains(l, "-h");
95 }
96
97 static int update_argv(const char name[], size_t l) {
98 static int can_do = -1;
99 int r;
100
101 assert(name);
102 assert(l < SIZE_MAX);
103
104 if (can_do == 0)
105 return 0;
106 can_do = false; /* We'll set it to true only if the whole process works */
107
108 /* Calling prctl() with PR_SET_MM_ARG_{START,END} requires CAP_SYS_RESOURCE so let's use this as quick bypass
109 * check, to avoid calling mmap() should PR_SET_MM_ARG_{START,END} fail with EPERM later on anyway. */
110 r = have_effective_cap(CAP_SYS_RESOURCE);
111 if (r < 0)
112 return log_debug_errno(r, "Failed to check if we have enough privileges: %m");
113 if (r == 0)
114 return log_debug_errno(SYNTHETIC_ERRNO(EPERM),
115 "Skipping PR_SET_MM, as we don't have privileges.");
116
117 static size_t mm_size = 0;
118 static char *mm = NULL;
119
120 if (mm_size < l+1) {
121 size_t nn_size;
122 char *nn;
123
124 nn_size = PAGE_ALIGN(l+1);
125 if (nn_size >= SIZE_MAX)
126 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "The requested argument is too long.");
127
128 nn = mmap(NULL, nn_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
129 if (nn == MAP_FAILED)
130 return log_debug_errno(errno, "mmap() failed: %m");
131
132 strncpy(nn, name, nn_size);
133
134 /* Now, let's tell the kernel about this new memory */
135 if (prctl(PR_SET_MM, PR_SET_MM_ARG_START, (unsigned long) nn, 0, 0) < 0) {
136 if (ERRNO_IS_PRIVILEGE(errno))
137 return log_debug_errno(errno, "PR_SET_MM_ARG_START failed: %m");
138
139 /* HACK: prctl() API is kind of dumb on this point. The existing end address may already be
140 * below the desired start address, in which case the kernel may have kicked this back due
141 * to a range-check failure (see linux/kernel/sys.c:validate_prctl_map() to see this in
142 * action). The proper solution would be to have a prctl() API that could set both start+end
143 * simultaneously, or at least let us query the existing address to anticipate this condition
144 * and respond accordingly. For now, we can only guess at the cause of this failure and try
145 * a workaround--which will briefly expand the arg space to something potentially huge before
146 * resizing it to what we want. */
147 log_debug_errno(errno, "PR_SET_MM_ARG_START failed, attempting PR_SET_MM_ARG_END hack: %m");
148
149 if (prctl(PR_SET_MM, PR_SET_MM_ARG_END, (unsigned long) nn + l + 1, 0, 0) < 0) {
150 r = log_debug_errno(errno, "PR_SET_MM_ARG_END hack failed, proceeding without: %m");
151 (void) munmap(nn, nn_size);
152 return r;
153 }
154
155 if (prctl(PR_SET_MM, PR_SET_MM_ARG_START, (unsigned long) nn, 0, 0) < 0)
156 return log_debug_errno(errno, "PR_SET_MM_ARG_START still failed, proceeding without: %m");
157 } else {
158 /* And update the end pointer to the new end, too. If this fails, we don't really know what
159 * to do, it's pretty unlikely that we can rollback, hence we'll just accept the failure,
160 * and continue. */
161 if (prctl(PR_SET_MM, PR_SET_MM_ARG_END, (unsigned long) nn + l + 1, 0, 0) < 0)
162 log_debug_errno(errno, "PR_SET_MM_ARG_END failed, proceeding without: %m");
163 }
164
165 if (mm)
166 (void) munmap(mm, mm_size);
167
168 mm = nn;
169 mm_size = nn_size;
170 } else {
171 strncpy(mm, name, mm_size);
172
173 /* Update the end pointer, continuing regardless of any failure. */
174 if (prctl(PR_SET_MM, PR_SET_MM_ARG_END, (unsigned long) mm + l + 1, 0, 0) < 0)
175 log_debug_errno(errno, "PR_SET_MM_ARG_END failed, proceeding without: %m");
176 }
177
178 can_do = true;
179 return 0;
180 }
181
182 int rename_process_full(const char *comm, const char *invocation) {
183 bool truncated = false;
184
185 /* This is a like a poor man's setproctitle(). It changes the comm field by the name specified by
186 * 'comm', and changes argv[0] and the glibc's internally used names of the process
187 * (program_invocation_name and program_invocation_short_name) by the name specified by 'invocation'.
188 * For the first one a limit of 16 chars applies; to the second one in many cases one of 10 (i.e.
189 * length of "/sbin/init") — however if we have CAP_SYS_RESOURCES it is unbounded; to the third one
190 * 7 (i.e. the length of "systemd". If you pass a longer string it will likely be truncated.
191 *
192 * Returns 0 if a name was set but truncated, > 0 if it was set but not truncated. */
193
194 if (isempty(comm))
195 return -EINVAL; /* let's not confuse users unnecessarily with an empty name */
196
197 if (!is_main_thread())
198 return -EPERM; /* Let's not allow setting the process name from other threads than the main one, as we
199 * cache things without locking, and we make assumptions that PR_SET_NAME sets the
200 * process name that isn't correct on any other threads */
201
202 size_t l = strlen(comm);
203
204 /* First step, change the comm field. The main thread's comm is identical to the process comm. This means we
205 * can use PR_SET_NAME, which sets the thread name for the calling thread. */
206 if (prctl(PR_SET_NAME, comm) < 0)
207 log_debug_errno(errno, "PR_SET_NAME failed: %m");
208 if (l >= TASK_COMM_LEN) /* Linux userspace process names can be 15 chars at max */
209 truncated = true;
210
211 /* If nothing specified, fall back to comm. */
212 if (isempty(invocation))
213 invocation = comm;
214
215 l = strlen(invocation);
216
217 /* Second step, change glibc's ID of the process name. */
218 if (program_invocation_name) {
219 size_t k;
220
221 k = strlen(program_invocation_name);
222 strncpy(program_invocation_name, invocation, k);
223 if (l > k)
224 truncated = true;
225
226 /* Also update the short name. */
227 char *p = strrchr(program_invocation_name, '/');
228 program_invocation_short_name = p ? p + 1 : program_invocation_name;
229 }
230
231 /* Third step, completely replace the argv[] array the kernel maintains for us. This requires privileges, but
232 * has the advantage that the argv[] array is exactly what we want it to be, and not filled up with zeros at
233 * the end. This is the best option for changing /proc/self/cmdline. */
234 (void) update_argv(invocation, l);
235
236 /* Fourth step: in all cases we'll also update the original argv[], so that our own code gets it right too if
237 * it still looks here */
238 if (saved_argc > 0) {
239 if (saved_argv[0]) {
240 size_t k;
241
242 k = strlen(saved_argv[0]);
243 strncpy(saved_argv[0], invocation, k);
244 if (l > k)
245 truncated = true;
246 }
247
248 for (int i = 1; i < saved_argc; i++) {
249 if (!saved_argv[i])
250 break;
251
252 memzero(saved_argv[i], strlen(saved_argv[i]));
253 }
254 }
255
256 return !truncated;
257 }