Debian Codegraph @

src/shared/acl-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <sys/syslog.h>
5
6 #include "sd-dlopen.h"
7
8 #include "acl-util.h"
9 #include "alloc-util.h"
10 #include "errno-util.h"
11 #include "extract-word.h"
12 #include "fd-util.h"
13 #include "log.h" /* IWYU pragma: keep */
14 #include "set.h"
15 #include "string-util.h"
16 #include "strv.h"
17 #include "user-util.h"
18
19 #if HAVE_ACL
20 DLSYM_PROTOTYPE(acl_add_perm);
21 DLSYM_PROTOTYPE(acl_calc_mask);
22 DLSYM_PROTOTYPE(acl_copy_entry);
23 DLSYM_PROTOTYPE(acl_create_entry);
24 DLSYM_PROTOTYPE(acl_delete_entry);
25 DLSYM_PROTOTYPE(acl_delete_perm);
26 DLSYM_PROTOTYPE(acl_dup);
27 DLSYM_PROTOTYPE(acl_entries);
28 DLSYM_PROTOTYPE(acl_extended_file);
29 DLSYM_PROTOTYPE(acl_free);
30 DLSYM_PROTOTYPE(acl_from_mode);
31 DLSYM_PROTOTYPE(acl_from_text);
32 DLSYM_PROTOTYPE(acl_get_entry);
33 DLSYM_PROTOTYPE(acl_get_fd);
34 DLSYM_PROTOTYPE(acl_get_file);
35 DLSYM_PROTOTYPE(acl_get_perm);
36 DLSYM_PROTOTYPE(acl_get_permset);
37 DLSYM_PROTOTYPE(acl_get_qualifier);
38 DLSYM_PROTOTYPE(acl_get_tag_type);
39 DLSYM_PROTOTYPE(acl_init);
40 DLSYM_PROTOTYPE(acl_set_fd);
41 DLSYM_PROTOTYPE(acl_set_file);
42 DLSYM_PROTOTYPE(acl_set_permset);
43 DLSYM_PROTOTYPE(acl_set_qualifier);
44 DLSYM_PROTOTYPE(acl_set_tag_type);
45 DLSYM_PROTOTYPE(acl_to_any_text);
46 #endif
47
48 int dlopen_libacl(int log_level) {
49 #if HAVE_ACL
50 static void *libacl_dl = NULL;
51
52 LIBACL_NOTE(SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
53
54 return dlopen_many_sym_or_warn(
55 &libacl_dl,
56 "libacl.so.1",
57 log_level,
58 DLSYM_ARG(acl_add_perm),
59 DLSYM_ARG(acl_calc_mask),
60 DLSYM_ARG(acl_copy_entry),
61 DLSYM_ARG(acl_create_entry),
62 DLSYM_ARG(acl_delete_entry),
63 DLSYM_ARG(acl_delete_perm),
64 DLSYM_ARG(acl_dup),
65 DLSYM_ARG(acl_entries),
66 DLSYM_ARG(acl_extended_file),
67 DLSYM_ARG(acl_free),
68 DLSYM_ARG(acl_from_mode),
69 DLSYM_ARG(acl_from_text),
70 DLSYM_ARG(acl_get_entry),
71 DLSYM_ARG(acl_get_fd),
72 DLSYM_ARG(acl_get_file),
73 DLSYM_ARG(acl_get_perm),
74 DLSYM_ARG(acl_get_permset),
75 DLSYM_ARG(acl_get_qualifier),
76 DLSYM_ARG(acl_get_tag_type),
77 DLSYM_ARG(acl_init),
78 DLSYM_ARG(acl_set_fd),
79 DLSYM_ARG(acl_set_file),
80 DLSYM_ARG(acl_set_permset),
81 DLSYM_ARG(acl_set_qualifier),
82 DLSYM_ARG(acl_set_tag_type),
83 DLSYM_ARG(acl_to_any_text));
84 #else
85 return log_full_errno(log_level, SYNTHETIC_ERRNO(EOPNOTSUPP),
86 "libacl support is not compiled in.");
87 #endif
88 }
89
90 #if HAVE_ACL
91 int devnode_acl(int fd, const Set *uids) {
92 _cleanup_set_free_ Set *found = NULL;
93 bool changed = false;
94 int r;
95
96 assert(fd >= 0);
97
98 r = dlopen_libacl(LOG_DEBUG);
99 if (r < 0)
100 return r;
101
102 _cleanup_(acl_freep) acl_t acl = NULL;
103 acl = sym_acl_get_file(FORMAT_PROC_FD_PATH(fd), ACL_TYPE_ACCESS);
104 if (!acl)
105 return -errno;
106
107 acl_entry_t entry;
108 for (r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
109 r > 0;
110 r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &entry)) {
111
112 acl_tag_t tag;
113 if (sym_acl_get_tag_type(entry, &tag) < 0)
114 return -errno;
115
116 if (tag != ACL_USER)
117 continue;
118
119 if (!set_isempty(uids)) {
120 uid_t *u = sym_acl_get_qualifier(entry);
121 if (!u)
122 return -errno;
123
124 if (set_contains(uids, UID_TO_PTR(*u))) {
125 acl_permset_t permset;
126 if (sym_acl_get_permset(entry, &permset) < 0)
127 return -errno;
128
129 int rd = sym_acl_get_perm(permset, ACL_READ);
130 if (rd < 0)
131 return -errno;
132
133 int wt = sym_acl_get_perm(permset, ACL_WRITE);
134 if (wt < 0)
135 return -errno;
136
137 if (!rd || !wt) {
138 if (sym_acl_add_perm(permset, ACL_READ|ACL_WRITE) < 0)
139 return -errno;
140
141 changed = true;
142 }
143
144 r = set_ensure_put(&found, NULL, UID_TO_PTR(*u));
145 if (r < 0)
146 return r;
147
148 continue;
149 }
150 }
151
152 if (sym_acl_delete_entry(acl, entry) < 0)
153 return -errno;
154
155 changed = true;
156 }
157 if (r < 0)
158 return -errno;
159
160 void *p;
161 SET_FOREACH(p, uids) {
162 uid_t uid = PTR_TO_UID(p);
163
164 if (uid == 0)
165 continue;
166
167 if (set_contains(found, UID_TO_PTR(uid)))
168 continue;
169
170 if (sym_acl_create_entry(&acl, &entry) < 0)
171 return -errno;
172
173 if (sym_acl_set_tag_type(entry, ACL_USER) < 0)
174 return -errno;
175
176 if (sym_acl_set_qualifier(entry, &uid) < 0)
177 return -errno;
178
179 acl_permset_t permset;
180 if (sym_acl_get_permset(entry, &permset) < 0)
181 return -errno;
182
183 if (sym_acl_add_perm(permset, ACL_READ|ACL_WRITE) < 0)
184 return -errno;
185
186 changed = true;
187 }
188
189 if (!changed)
190 return 0;
191
192 if (sym_acl_calc_mask(&acl) < 0)
193 return -errno;
194
195 if (sym_acl_set_file(FORMAT_PROC_FD_PATH(fd), ACL_TYPE_ACCESS, acl) < 0)
196 return -errno;
197
198 return 0;
199 }
200
201 static int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *ret_entry) {
202 acl_entry_t i;
203 int r;
204
205 assert(acl);
206 assert(uid_is_valid(uid));
207 assert(ret_entry);
208
209 for (r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
210 r > 0;
211 r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
212
213 acl_tag_t tag;
214 bool b;
215
216 if (sym_acl_get_tag_type(i, &tag) < 0)
217 return -errno;
218
219 if (tag != ACL_USER)
220 continue;
221
222 _cleanup_(acl_free_uid_tpp) uid_t *u = NULL;
223 u = sym_acl_get_qualifier(i);
224 if (!u)
225 return -errno;
226
227 b = *u == uid;
228 if (b) {
229 *ret_entry = i;
230 return 1;
231 }
232 }
233 if (r < 0)
234 return -errno;
235
236 *ret_entry = NULL;
237 return 0;
238 }
239
240 int calc_acl_mask_if_needed(acl_t *acl_p) {
241 acl_entry_t i;
242 int r;
243 bool need = false;
244
245 assert(acl_p);
246
247 for (r = sym_acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
248 r > 0;
249 r = sym_acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
250 acl_tag_t tag;
251
252 if (sym_acl_get_tag_type(i, &tag) < 0)
253 return -errno;
254
255 if (tag == ACL_MASK)
256 return 0;
257
258 if (IN_SET(tag, ACL_USER, ACL_GROUP))
259 need = true;
260 }
261 if (r < 0)
262 return -errno;
263
264 if (need && sym_acl_calc_mask(acl_p) < 0)
265 return -errno;
266
267 return need;
268 }
269
270 int add_base_acls_if_needed(acl_t *acl_p, const char *path) {
271 acl_entry_t i;
272 int r;
273 bool have_user_obj = false, have_group_obj = false, have_other = false;
274 struct stat st;
275 _cleanup_(acl_freep) acl_t basic = NULL;
276
277 assert(acl_p);
278 assert(path);
279
280 for (r = sym_acl_get_entry(*acl_p, ACL_FIRST_ENTRY, &i);
281 r > 0;
282 r = sym_acl_get_entry(*acl_p, ACL_NEXT_ENTRY, &i)) {
283 acl_tag_t tag;
284
285 if (sym_acl_get_tag_type(i, &tag) < 0)
286 return -errno;
287
288 if (tag == ACL_USER_OBJ)
289 have_user_obj = true;
290 else if (tag == ACL_GROUP_OBJ)
291 have_group_obj = true;
292 else if (tag == ACL_OTHER)
293 have_other = true;
294 if (have_user_obj && have_group_obj && have_other)
295 return 0;
296 }
297 if (r < 0)
298 return -errno;
299
300 if (stat(path, &st) < 0)
301 return -errno;
302
303 basic = sym_acl_from_mode(st.st_mode);
304 if (!basic)
305 return -errno;
306
307 for (r = sym_acl_get_entry(basic, ACL_FIRST_ENTRY, &i);
308 r > 0;
309 r = sym_acl_get_entry(basic, ACL_NEXT_ENTRY, &i)) {
310 acl_tag_t tag;
311 acl_entry_t dst;
312
313 if (sym_acl_get_tag_type(i, &tag) < 0)
314 return -errno;
315
316 if ((tag == ACL_USER_OBJ && have_user_obj) ||
317 (tag == ACL_GROUP_OBJ && have_group_obj) ||
318 (tag == ACL_OTHER && have_other))
319 continue;
320
321 r = sym_acl_create_entry(acl_p, &dst);
322 if (r < 0)
323 return -errno;
324
325 r = sym_acl_copy_entry(dst, i);
326 if (r < 0)
327 return -errno;
328 }
329 if (r < 0)
330 return -errno;
331 return 0;
332 }
333
334 int acl_search_groups(const char *path, char ***ret_groups) {
335 _cleanup_strv_free_ char **g = NULL;
336 _cleanup_(acl_freep) acl_t acl = NULL;
337 bool ret = false;
338 acl_entry_t entry;
339 int r;
340
341 assert(path);
342
343 r = dlopen_libacl(LOG_DEBUG);
344 if (r < 0)
345 return r;
346
347 acl = sym_acl_get_file(path, ACL_TYPE_DEFAULT);
348 if (!acl)
349 return -errno;
350
351 r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &entry);
352 for (;;) {
353 _cleanup_(acl_free_gid_tpp) gid_t *gid = NULL;
354 acl_tag_t tag;
355
356 if (r < 0)
357 return -errno;
358 if (r == 0)
359 break;
360
361 if (sym_acl_get_tag_type(entry, &tag) < 0)
362 return -errno;
363
364 if (tag != ACL_GROUP)
365 goto next;
366
367 gid = sym_acl_get_qualifier(entry);
368 if (!gid)
369 return -errno;
370
371 if (in_gid(*gid) > 0) {
372 if (!ret_groups)
373 return true;
374
375 ret = true;
376 }
377
378 if (ret_groups) {
379 char *name;
380
381 name = gid_to_name(*gid);
382 if (!name)
383 return -ENOMEM;
384
385 r = strv_consume(&g, name);
386 if (r < 0)
387 return r;
388 }
389
390 next:
391 r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &entry);
392 }
393
394 if (ret_groups)
395 *ret_groups = TAKE_PTR(g);
396
397 return ret;
398 }
399
400 int parse_acl(
401 const char *text,
402 acl_t *ret_acl_access,
403 acl_t *ret_acl_access_exec, /* extra rules to apply to inodes subject to uppercase X handling */
404 acl_t *ret_acl_default,
405 bool want_mask) {
406
407 _cleanup_strv_free_ char **a = NULL, **e = NULL, **d = NULL, **split = NULL;
408 _cleanup_(acl_freep) acl_t a_acl = NULL, e_acl = NULL, d_acl = NULL;
409 int r;
410
411 assert(text);
412 assert(ret_acl_access);
413 assert(ret_acl_access_exec);
414 assert(ret_acl_default);
415
416 split = strv_split(text, ",");
417 if (!split)
418 return -ENOMEM;
419
420 r = dlopen_libacl(LOG_DEBUG);
421 if (r < 0)
422 return r;
423
424 STRV_FOREACH(entry, split) {
425 _cleanup_strv_free_ char **entry_split = NULL;
426 _cleanup_free_ char *entry_join = NULL;
427 int n;
428
429 n = strv_split_full(&entry_split, *entry, ":", EXTRACT_DONT_COALESCE_SEPARATORS|EXTRACT_RETAIN_ESCAPE);
430 if (n < 0)
431 return n;
432
433 if (n < 3 || n > 4)
434 return -EINVAL;
435
436 string_replace_char(entry_split[n-1], 'X', 'x');
437
438 if (n == 4) {
439 if (!STR_IN_SET(entry_split[0], "default", "d"))
440 return -EINVAL;
441
442 entry_join = strv_join(entry_split + 1, ":");
443 if (!entry_join)
444 return -ENOMEM;
445
446 r = strv_consume(&d, TAKE_PTR(entry_join));
447 } else { /* n == 3 */
448 entry_join = strv_join(entry_split, ":");
449 if (!entry_join)
450 return -ENOMEM;
451
452 if (!streq(*entry, entry_join))
453 r = strv_consume(&e, TAKE_PTR(entry_join));
454 else
455 r = strv_consume(&a, TAKE_PTR(entry_join));
456 }
457 if (r < 0)
458 return r;
459 }
460
461 if (!strv_isempty(a)) {
462 _cleanup_free_ char *join = NULL;
463
464 join = strv_join(a, ",");
465 if (!join)
466 return -ENOMEM;
467
468 a_acl = sym_acl_from_text(join);
469 if (!a_acl)
470 return -errno;
471
472 if (want_mask) {
473 r = calc_acl_mask_if_needed(&a_acl);
474 if (r < 0)
475 return r;
476 }
477 }
478
479 if (!strv_isempty(e)) {
480 _cleanup_free_ char *join = NULL;
481
482 join = strv_join(e, ",");
483 if (!join)
484 return -ENOMEM;
485
486 e_acl = sym_acl_from_text(join);
487 if (!e_acl)
488 return -errno;
489
490 /* The mask must be calculated after deciding whether the execute bit should be set. */
491 }
492
493 if (!strv_isempty(d)) {
494 _cleanup_free_ char *join = NULL;
495
496 join = strv_join(d, ",");
497 if (!join)
498 return -ENOMEM;
499
500 d_acl = sym_acl_from_text(join);
501 if (!d_acl)
502 return -errno;
503
504 if (want_mask) {
505 r = calc_acl_mask_if_needed(&d_acl);
506 if (r < 0)
507 return r;
508 }
509 }
510
511 *ret_acl_access = TAKE_PTR(a_acl);
512 *ret_acl_access_exec = TAKE_PTR(e_acl);
513 *ret_acl_default = TAKE_PTR(d_acl);
514
515 return 0;
516 }
517
518 static int acl_entry_equal(acl_entry_t a, acl_entry_t b) {
519 acl_tag_t tag_a, tag_b;
520
521 if (sym_acl_get_tag_type(a, &tag_a) < 0)
522 return -errno;
523
524 if (sym_acl_get_tag_type(b, &tag_b) < 0)
525 return -errno;
526
527 if (tag_a != tag_b)
528 return false;
529
530 switch (tag_a) {
531 case ACL_USER_OBJ:
532 case ACL_GROUP_OBJ:
533 case ACL_MASK:
534 case ACL_OTHER:
535 /* can have only one of those */
536 return true;
537 case ACL_USER: {
538 _cleanup_(acl_free_uid_tpp) uid_t *uid_a = NULL, *uid_b = NULL;
539
540 uid_a = sym_acl_get_qualifier(a);
541 if (!uid_a)
542 return -errno;
543
544 uid_b = sym_acl_get_qualifier(b);
545 if (!uid_b)
546 return -errno;
547
548 return *uid_a == *uid_b;
549 }
550 case ACL_GROUP: {
551 _cleanup_(acl_free_gid_tpp) gid_t *gid_a = NULL, *gid_b = NULL;
552
553 gid_a = sym_acl_get_qualifier(a);
554 if (!gid_a)
555 return -errno;
556
557 gid_b = sym_acl_get_qualifier(b);
558 if (!gid_b)
559 return -errno;
560
561 return *gid_a == *gid_b;
562 }
563 default:
564 assert_not_reached();
565 }
566 }
567
568 static int find_acl_entry(acl_t acl, acl_entry_t entry, acl_entry_t *ret) {
569 acl_entry_t i;
570 int r;
571
572 for (r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
573 r > 0;
574 r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
575
576 r = acl_entry_equal(i, entry);
577 if (r < 0)
578 return r;
579 if (r > 0) {
580 if (ret)
581 *ret = i;
582 return 0;
583 }
584 }
585 if (r < 0)
586 return -errno;
587
588 return -ENOENT;
589 }
590
591 int acls_for_file(const char *path, acl_type_t type, acl_t acl, acl_t *ret) {
592 _cleanup_(acl_freep) acl_t applied = NULL;
593 acl_entry_t i;
594 int r;
595
596 assert(path);
597
598 r = dlopen_libacl(LOG_DEBUG);
599 if (r < 0)
600 return r;
601
602 applied = sym_acl_get_file(path, type);
603 if (!applied)
604 return -errno;
605
606 for (r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
607 r > 0;
608 r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
609
610 acl_entry_t j;
611
612 r = find_acl_entry(applied, i, &j);
613 if (r == -ENOENT) {
614 if (sym_acl_create_entry(&applied, &j) < 0)
615 return -errno;
616 } else if (r < 0)
617 return r;
618
619 if (sym_acl_copy_entry(j, i) < 0)
620 return -errno;
621 }
622 if (r < 0)
623 return -errno;
624
625 if (ret)
626 *ret = TAKE_PTR(applied);
627
628 return 0;
629 }
630
631 /* POSIX says that ACL_{READ,WRITE,EXECUTE} don't have to be bitmasks. But that is a natural thing to do and
632 * all extant implementations do it. Let's make sure that we fail verbosely in the (imho unlikely) scenario
633 * that we get a new implementation that does not satisfy this. */
634 assert_cc(!(ACL_READ & ACL_WRITE));
635 assert_cc(!(ACL_WRITE & ACL_EXECUTE));
636 assert_cc(!(ACL_EXECUTE & ACL_READ));
637 assert_cc((unsigned) ACL_READ == ACL_READ);
638 assert_cc((unsigned) ACL_WRITE == ACL_WRITE);
639 assert_cc((unsigned) ACL_EXECUTE == ACL_EXECUTE);
640
641 int fd_add_uid_acl_permission(
642 int fd,
643 uid_t uid,
644 unsigned mask) {
645
646 _cleanup_(acl_freep) acl_t acl = NULL;
647 acl_permset_t permset;
648 acl_entry_t entry;
649 int r;
650
651 /* Adds an ACL entry for the specified file to allow the indicated access to the specified
652 * user. Operates purely incrementally. */
653
654 assert(fd >= 0);
655 assert(uid_is_valid(uid));
656
657 r = dlopen_libacl(LOG_DEBUG);
658 if (r < 0)
659 return r;
660
661 acl = sym_acl_get_fd(fd);
662 if (!acl)
663 return -errno;
664
665 r = acl_find_uid(acl, uid, &entry);
666 if (r <= 0) {
667 if (sym_acl_create_entry(&acl, &entry) < 0 ||
668 sym_acl_set_tag_type(entry, ACL_USER) < 0 ||
669 sym_acl_set_qualifier(entry, &uid) < 0)
670 return -errno;
671 }
672
673 if (sym_acl_get_permset(entry, &permset) < 0)
674 return -errno;
675
676 if ((mask & ACL_READ) && sym_acl_add_perm(permset, ACL_READ) < 0)
677 return -errno;
678 if ((mask & ACL_WRITE) && sym_acl_add_perm(permset, ACL_WRITE) < 0)
679 return -errno;
680 if ((mask & ACL_EXECUTE) && sym_acl_add_perm(permset, ACL_EXECUTE) < 0)
681 return -errno;
682
683 r = calc_acl_mask_if_needed(&acl);
684 if (r < 0)
685 return r;
686
687 if (sym_acl_set_fd(fd, acl) < 0)
688 return -errno;
689
690 return 0;
691 }
692 #endif
693
694 static int fd_acl_make_read_only_fallback(int fd) {
695 struct stat st;
696
697 assert(fd >= 0);
698
699 if (fstat(fd, &st) < 0)
700 return -errno;
701
702 if ((st.st_mode & 0222) == 0)
703 return 0;
704
705 if (fchmod(fd, st.st_mode & 0555) < 0)
706 return -errno;
707
708 return 1;
709 }
710
711 int fd_acl_make_read_only(int fd) {
712 assert(fd >= 0);
713
714 #if HAVE_ACL
715 _cleanup_(acl_freep) acl_t acl = NULL;
716 bool changed = false;
717 acl_entry_t i;
718 int r;
719
720 /* Safely drops all W bits from all relevant ACL entries of the file, without changing entries which
721 * are masked by the ACL mask */
722
723 r = dlopen_libacl(LOG_DEBUG);
724 if (r < 0)
725 goto maybe_fallback;
726
727 acl = sym_acl_get_fd(fd);
728 if (!acl) {
729 r = -errno;
730 goto maybe_fallback;
731 }
732
733 for (r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
734 r > 0;
735 r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
736 acl_permset_t permset;
737 acl_tag_t tag;
738 int b;
739
740 if (sym_acl_get_tag_type(i, &tag) < 0)
741 return -errno;
742
743 /* These three control the x bits overall (as ACL_MASK affects all remaining tags) */
744 if (!IN_SET(tag, ACL_USER_OBJ, ACL_MASK, ACL_OTHER))
745 continue;
746
747 if (sym_acl_get_permset(i, &permset) < 0)
748 return -errno;
749
750 b = sym_acl_get_perm(permset, ACL_WRITE);
751 if (b < 0)
752 return -errno;
753
754 if (b) {
755 if (sym_acl_delete_perm(permset, ACL_WRITE) < 0)
756 return -errno;
757
758 changed = true;
759 }
760 }
761 if (r < 0)
762 return -errno;
763
764 if (!changed)
765 return 0;
766
767 if (sym_acl_set_fd(fd, acl) < 0) {
768 r = -errno;
769 goto maybe_fallback;
770 }
771
772 return 1;
773
774 maybe_fallback:
775 if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
776 return r;
777 #endif
778
779 /* No ACLs? Then just update the regular mode_t */
780 return fd_acl_make_read_only_fallback(fd);
781 }
782
783 static int fd_acl_make_writable_fallback(int fd) {
784 struct stat st;
785
786 assert(fd >= 0);
787
788 if (fstat(fd, &st) < 0)
789 return -errno;
790
791 if ((st.st_mode & 0200) != 0) /* already set */
792 return 0;
793
794 if (fchmod(fd, (st.st_mode & 07777) | 0200) < 0)
795 return -errno;
796
797 return 1;
798 }
799
800 int fd_acl_make_writable(int fd) {
801 assert(fd >= 0);
802
803 #if HAVE_ACL
804 _cleanup_(acl_freep) acl_t acl = NULL;
805 acl_entry_t i;
806 int r;
807
808 /* Safely adds the writable bit to the owner's ACL entry of this inode. (And only the owner's! – This
809 * not the obvious inverse of fd_acl_make_read_only() hence!) */
810
811 r = dlopen_libacl(LOG_DEBUG);
812 if (r < 0)
813 goto maybe_fallback;
814
815 acl = sym_acl_get_fd(fd);
816 if (!acl) {
817 r = -errno;
818 goto maybe_fallback;
819 }
820
821 for (r = sym_acl_get_entry(acl, ACL_FIRST_ENTRY, &i);
822 r > 0;
823 r = sym_acl_get_entry(acl, ACL_NEXT_ENTRY, &i)) {
824 acl_permset_t permset;
825 acl_tag_t tag;
826 int b;
827
828 if (sym_acl_get_tag_type(i, &tag) < 0)
829 return -errno;
830
831 if (tag != ACL_USER_OBJ)
832 continue;
833
834 if (sym_acl_get_permset(i, &permset) < 0)
835 return -errno;
836
837 b = sym_acl_get_perm(permset, ACL_WRITE);
838 if (b < 0)
839 return -errno;
840
841 if (b)
842 return 0; /* Already set? Then there's nothing to do. */
843
844 if (sym_acl_add_perm(permset, ACL_WRITE) < 0)
845 return -errno;
846
847 break;
848 }
849 if (r < 0)
850 return -errno;
851
852 if (sym_acl_set_fd(fd, acl) < 0) {
853 r = -errno;
854 goto maybe_fallback;
855 }
856
857 return 1;
858
859 maybe_fallback:
860 if (!ERRNO_IS_NEG_NOT_SUPPORTED(r))
861 return r;
862 #endif
863
864 /* No ACLs? Then just update the regular mode_t */
865 return fd_acl_make_writable_fallback(fd);
866 }
867
868 int inode_type_can_acl(mode_t mode) {
869 return IN_SET(mode & S_IFMT, S_IFSOCK, S_IFREG, S_IFBLK, S_IFCHR, S_IFDIR, S_IFIFO);
870 }