Commit | Line | Data |
---|---|---|
0fdd1e2c | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
3 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
0fdd1e2c | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
0fdd1e2c | 6 | * |
0fdd1e2c DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
0fdd1e2c DG |
10 | #include <fcntl.h> |
11 | #include <limits.h> | |
12 | #include <sys/mman.h> | |
13 | #include <sys/stat.h> | |
14 | #include <sys/types.h> | |
15 | #include <sys/wait.h> | |
16 | #include <unistd.h> | |
17 | #include <urcu.h> | |
18 | ||
db758600 | 19 | #include <common/error.h> |
0fdd1e2c DG |
20 | |
21 | #include "shm.h" | |
22 | ||
23 | /* | |
24 | * Using fork to set umask in the child process (not multi-thread safe). We | |
25 | * deal with the shm_open vs ftruncate race (happening when the sessiond owns | |
26 | * the shm and does not let everybody modify it, to ensure safety against | |
27 | * shm_unlink) by simply letting the mmap fail and retrying after a few | |
28 | * seconds. For global shm, everybody has rw access to it until the sessiond | |
29 | * starts. | |
30 | */ | |
31 | static int get_wait_shm(char *shm_path, size_t mmap_size, int global) | |
32 | { | |
33 | int wait_shm_fd, ret; | |
0fdd1e2c DG |
34 | mode_t mode; |
35 | ||
0525e9ae DG |
36 | assert(shm_path); |
37 | ||
0fdd1e2c DG |
38 | /* Default permissions */ |
39 | mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP; | |
40 | ||
7972d619 DG |
41 | /* |
42 | * Change owner of the shm path. | |
43 | */ | |
0fdd1e2c | 44 | if (global) { |
0fdd1e2c | 45 | /* |
7972d619 DG |
46 | * If global session daemon, any application can |
47 | * register. Make it initially writeable so applications | |
48 | * registering concurrently can do ftruncate() by | |
49 | * themselves. | |
0fdd1e2c | 50 | */ |
7972d619 | 51 | mode |= S_IROTH | S_IWOTH; |
0fdd1e2c DG |
52 | } |
53 | ||
54 | /* | |
7d051034 DG |
55 | * We're alone in a child process, so we can modify the process-wide |
56 | * umask. | |
0fdd1e2c | 57 | */ |
7d051034 | 58 | umask(~mode); |
0fdd1e2c | 59 | |
7d051034 DG |
60 | /* |
61 | * Try creating shm (or get rw access). We don't do an exclusive open, | |
62 | * because we allow other processes to create+ftruncate it concurrently. | |
cf86ff2c JG |
63 | * |
64 | * A sysctl, fs.protected_regular may prevent the session daemon from | |
65 | * opening a previously created shm when the O_CREAT flag is provided. | |
66 | * Systemd enables this ABI-breaking change by default since v241. | |
67 | * | |
68 | * First, attempt to use the create-or-open semantic that is | |
69 | * desired here. If this fails with EACCES, work around this broken | |
70 | * behaviour and attempt to open the shm without the O_CREAT flag. | |
71 | * | |
72 | * The two attempts are made in this order since applications are | |
73 | * expected to race with the session daemon to create this shm. | |
74 | * Attempting an shm_open() without the O_CREAT flag first could fail | |
75 | * because the file doesn't exist. It could then be created by an | |
76 | * application, which would cause a second try with the O_CREAT flag to | |
77 | * fail with EACCES. | |
78 | * | |
79 | * Note that this introduces a new failure mode where a user could | |
80 | * launch an application (creating the shm) and unlink the shm while | |
81 | * the session daemon is launching, causing the second attempt | |
82 | * to fail. This is not recovered-from as unlinking the shm will | |
83 | * prevent userspace tracing from succeeding anyhow: the sessiond would | |
84 | * use a now-unlinked shm, while the next application would create | |
85 | * a new named shm. | |
7d051034 DG |
86 | */ |
87 | wait_shm_fd = shm_open(shm_path, O_RDWR | O_CREAT, mode); | |
88 | if (wait_shm_fd < 0) { | |
cf86ff2c JG |
89 | if (errno == EACCES) { |
90 | /* Work around sysctl fs.protected_regular. */ | |
91 | DBG("shm_open of %s returned EACCES, this may be caused " | |
92 | "by the fs.protected_regular sysctl. " | |
93 | "Attempting to open the shm without " | |
94 | "creating it.", shm_path); | |
95 | wait_shm_fd = shm_open(shm_path, O_RDWR, mode); | |
96 | } | |
97 | if (wait_shm_fd < 0) { | |
98 | PERROR("Failed to open wait shm at %s", shm_path); | |
99 | goto error; | |
100 | } | |
7d051034 | 101 | } |
0fdd1e2c | 102 | |
7d051034 DG |
103 | ret = ftruncate(wait_shm_fd, mmap_size); |
104 | if (ret < 0) { | |
df0f840b | 105 | PERROR("ftruncate wait shm"); |
7d051034 DG |
106 | exit(EXIT_FAILURE); |
107 | } | |
0fdd1e2c | 108 | |
409a0c56 | 109 | #ifndef __FreeBSD__ |
7972d619 DG |
110 | if (global) { |
111 | ret = fchown(wait_shm_fd, 0, 0); | |
112 | if (ret < 0) { | |
113 | PERROR("fchown"); | |
114 | exit(EXIT_FAILURE); | |
115 | } | |
116 | /* | |
117 | * If global session daemon, any application can | |
118 | * register so the shm needs to be set in read-only mode | |
119 | * for others. | |
120 | */ | |
121 | mode &= ~S_IWOTH; | |
122 | ret = fchmod(wait_shm_fd, mode); | |
123 | if (ret < 0) { | |
124 | PERROR("fchmod"); | |
125 | exit(EXIT_FAILURE); | |
126 | } | |
127 | } else { | |
128 | ret = fchown(wait_shm_fd, getuid(), getgid()); | |
129 | if (ret < 0) { | |
130 | PERROR("fchown"); | |
131 | exit(EXIT_FAILURE); | |
132 | } | |
0fdd1e2c | 133 | } |
409a0c56 | 134 | #else |
7972d619 | 135 | #warning "FreeBSD does not support setting file mode on shm FD." |
409a0c56 | 136 | #endif |
0fdd1e2c | 137 | |
0fdd1e2c DG |
138 | DBG("Got the wait shm fd %d", wait_shm_fd); |
139 | ||
140 | return wait_shm_fd; | |
141 | ||
142 | error: | |
143 | DBG("Failing to get the wait shm fd"); | |
144 | ||
145 | return -1; | |
146 | } | |
147 | ||
148 | /* | |
149 | * Return the wait shm mmap for UST application notification. The global | |
150 | * variable is used to indicate if the the session daemon is global | |
151 | * (root:tracing) or running with an unprivileged user. | |
152 | * | |
153 | * This returned value is used by futex_wait_update() in futex.c to WAKE all | |
154 | * waiters which are UST application waiting for a session daemon. | |
155 | */ | |
156 | char *shm_ust_get_mmap(char *shm_path, int global) | |
157 | { | |
6c699394 | 158 | size_t mmap_size; |
0fdd1e2c DG |
159 | int wait_shm_fd, ret; |
160 | char *wait_shm_mmap; | |
6c699394 | 161 | long sys_page_size; |
0fdd1e2c | 162 | |
0525e9ae DG |
163 | assert(shm_path); |
164 | ||
6c699394 DG |
165 | sys_page_size = sysconf(_SC_PAGE_SIZE); |
166 | if (sys_page_size < 0) { | |
167 | PERROR("sysconf PAGE_SIZE"); | |
168 | goto error; | |
169 | } | |
170 | mmap_size = sys_page_size; | |
171 | ||
0fdd1e2c DG |
172 | wait_shm_fd = get_wait_shm(shm_path, mmap_size, global); |
173 | if (wait_shm_fd < 0) { | |
174 | goto error; | |
175 | } | |
176 | ||
177 | wait_shm_mmap = mmap(NULL, mmap_size, PROT_WRITE | PROT_READ, | |
178 | MAP_SHARED, wait_shm_fd, 0); | |
7d051034 | 179 | |
0fdd1e2c DG |
180 | /* close shm fd immediately after taking the mmap reference */ |
181 | ret = close(wait_shm_fd); | |
182 | if (ret) { | |
df0f840b | 183 | PERROR("Error closing fd"); |
0fdd1e2c DG |
184 | } |
185 | ||
186 | if (wait_shm_mmap == MAP_FAILED) { | |
187 | DBG("mmap error (can be caused by race with ust)."); | |
188 | goto error; | |
189 | } | |
190 | ||
191 | return wait_shm_mmap; | |
192 | ||
193 | error: | |
194 | return NULL; | |
195 | } |