Debian Codegraph @

src/util/sockopt.c

raw
1 /*
2 * Socket Options Helpers
3 */
4
5 #include <c-stdaux.h>
6 #include <grp.h>
7 #include <pwd.h>
8 #include <stdlib.h>
9 #include <sys/socket.h>
10 #include "catalog/catalog-ids.h"
11 #include "util/error.h"
12 #include "util/log.h"
13 #include "util/sockopt.h"
14
15 int sockopt_get_peersec(int fd, char **labelp, size_t *lenp) {
16 _c_cleanup_(c_freep) char *label = NULL;
17 socklen_t len = 1023;
18 char *l;
19 int r;
20
21 /*
22 * There is no way to know how big a result SO_PEERSEC returns. Hence,
23 * we simply keep re-allocating the buffer to the size returned by
24 * SO_PEERSEC on failure. Note that this is racy. The seclabel can
25 * change between calls. Hence, we must retry in a loop.
26 */
27
28 label = malloc(len + 1);
29 if (!label)
30 return error_origin(-ENOMEM);
31
32 for (;;) {
33 r = getsockopt(fd, SOL_SOCKET, SO_PEERSEC, label, &len);
34 if (r >= 0) {
35 label[len] = '\0';
36 break;
37 } else if (errno == ENOPROTOOPT) {
38 *label = 0;
39 break;
40 } else if (errno != ERANGE) {
41 return error_origin(-errno);
42 }
43
44 l = realloc(label, len + 1);
45 if (!l)
46 return error_origin(-ENOMEM);
47
48 label = l;
49 }
50
51 /* dup label to throw away the unnecessary padding bytes */
52 l = strdup(label);
53 if (!l)
54 return error_origin(-ENOMEM);
55
56 *labelp = l;
57 *lenp = strlen(l);
58 return 0;
59 }
60
61 static int gid_compare(const void *va, const void *vb) {
62 const gid_t *a = va, *b = vb;
63
64 if (*a < *b)
65 return -1;
66 else if (*a > *b)
67 return 1;
68 else
69 return 0;
70 }
71
72 int sockopt_get_peergroups(int fd, Log *log, uid_t uid, gid_t primary_gid, gid_t **gidsp, size_t *n_gidsp) {
73 _c_cleanup_(c_freep) gid_t *gids = NULL;
74 socklen_t socklen;
75 int r, n_gids, i, j;
76 void *tmp;
77
78 /*
79 * For compatibility to dbus-daemon(1), we need to know the auxiliary
80 * groups a peer is in. Otherwise, we would be unable to apply group
81 * policies. We use the SO_PEERGROUPS socket option to retrieve this
82 * data alongside the uid+gid we got via SO_PEERCREDS.
83 * SO_PEERGROUPS support was added in:
84 *
85 * commit 28b5ba2aa0f55d80adb2624564ed2b170c19519e
86 * Author: David Herrmann <[email protected]>
87 * Commit: David S. Miller <[email protected]>
88 * Date: Wed Jun 21 10:47:15 2017 +0200
89 *
90 * net: introduce SO_PEERGROUPS getsockopt
91 *
92 * You are highly recommended to run >=linux-4.13.
93 */
94 {
95 n_gids = 8;
96 socklen = n_gids * sizeof(*gids);
97
98 gids = malloc(sizeof(*gids) + socklen);
99 if (!gids)
100 return error_origin(-ENOMEM);
101 gids[0] = primary_gid;
102
103 r = getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, gids + 1, &socklen);
104 if (r < 0 && errno == ERANGE) {
105 tmp = realloc(gids, sizeof(*gids) + socklen);
106 if (!tmp)
107 return error_origin(-ENOMEM);
108 gids = tmp;
109 gids[0] = primary_gid;
110
111 r = getsockopt(fd, SOL_SOCKET, SO_PEERGROUPS, gids + 1, &socklen);
112 }
113 if (r < 0 && errno != ENOPROTOOPT) {
114 return error_origin(-errno);
115 } else if (r >= 0) {
116 n_gids = 1 + socklen / sizeof(*gids);
117
118 /* Sort and deduplicate for deterministic behavior. */
119 qsort(gids, n_gids, sizeof(*gids), gid_compare);
120 for (i = 1, j = 0; i < n_gids; ++i) {
121 if (gids[i] != gids[j])
122 gids[++j] = gids[i];
123 }
124
125 if (gidsp) {
126 *gidsp = gids;
127 gids = NULL;
128 }
129 if (n_gidsp)
130 *n_gidsp = n_gids;
131 return 0;
132 }
133
134 /* fallthrough */
135 }
136
137 /*
138 * If we end up here, then SO_PEERGROUPS did not work. This is not safe
139 * and you really better update your kernel to support SO_PEERGROUPS.
140 * Warn loudly before continuing with the fallback.
141 */
142 {
143 static bool warned;
144
145 if (!warned) {
146 warned = true;
147 log_append_here(log, LOG_ERR, 0, DBUS_BROKER_CATALOG_NO_SOPEERGROUP);
148 r = log_commitf(log, "Falling back to racy auxiliary groups"
149 "resolution using nss.\n");
150 if (r)
151 return error_fold(r);
152 }
153 }
154
155 /*
156 * If SO_PEERGROUPS is not available, we fall back to getgrouplist(3),
157 * which queries the user database. This is racy, incomplete, and in no
158 * way consistent with the credentials associated with the used
159 * resources. However, it is what was historically used, so we will
160 * keep it for some more time.
161 *
162 * XXX: At what point can we drop this? We keep it for now, but we
163 * should at some point make a specific kernel version a
164 * hard-requirement.
165 */
166 {
167 struct passwd *passwd;
168 int n_gids_previous;
169
170 passwd = getpwuid(uid);
171 if (!passwd)
172 return error_origin(-errno);
173
174 n_gids = 8;
175 do {
176 n_gids_previous = n_gids;
177 tmp = realloc(gids, sizeof(*gids) * n_gids);
178 if (!tmp)
179 return error_origin(-ENOMEM);
180
181 gids = tmp;
182 r = getgrouplist(passwd->pw_name, passwd->pw_gid, gids, &n_gids);
183 if (r == -1 && n_gids <= n_gids_previous)
184 return error_origin(-ENOTRECOVERABLE);
185 } while (r == -1);
186
187 if (gidsp) {
188 *gidsp = gids;
189 gids = NULL;
190 }
191 if (n_gidsp)
192 *n_gidsp = n_gids;
193 }
194
195 return 0;
196 }
197
198 /**
199 * sockopt_get_peerpidfd() - query pidfd of remote peer
200 * @fd: socket to operate on
201 * @pidfdp: output variable to store pidfd
202 *
203 * Query the given socket for the PID of the remote peer and return a
204 * pidfd linked to this PID. The file-desciptor is granted to the caller,
205 * which is responsible to close it when no longer in use.
206 *
207 * If the kernel does not support the underlying `SO_PEERPIDFD` socket
208 * option, SOCKOPT_E_UNSUPPORTED is returned.
209 *
210 * If the socket type does not support `SO_PEERPIDFD`, SOCKOPT_E_UNAVAILABLE
211 * is returned.
212 *
213 * If the target process was already reaped, SOCKOPT_E_REAPED is returned.
214 *
215 * Return: 0 on success, SOCKOPT_E_UNSUPPORTED if not supported by the
216 * running kernel, SOCKOPT_E_UNAVAILABLE if the socket type does not
217 * support the option, SOCKOPT_E_REAPED if the target process was
218 * already reaped, negative error code on failure.
219 */
220 int sockopt_get_peerpidfd(int fd, int *pidfdp) {
221 socklen_t socklen = sizeof(int);
222 int r, pidfd;
223
224 /* XXX: Drop this once we require `linux-api-headers >= 6.5` */
225 # ifndef SO_PEERPIDFD
226 # if defined(__parisc__)
227 # define SO_PEERPIDFD 0x404B
228 # elif defined(__sparc__)
229 # define SO_PEERPIDFD 0x0056
230 # else
231 # define SO_PEERPIDFD 77
232 # endif
233 # endif
234
235 r = getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD, &pidfd, &socklen);
236 if (r < 0) {
237 if (errno == ENOPROTOOPT)
238 return SOCKOPT_E_UNSUPPORTED;
239 if (errno == ENODATA)
240 return SOCKOPT_E_UNAVAILABLE;
241 if (errno == EINVAL || errno == ESRCH)
242 return SOCKOPT_E_REAPED;
243
244 return error_origin(-errno);
245 }
246
247 *pidfdp = pidfd;
248 return 0;
249 }