2 * Copyright 2009-10, 2013, 2017, 2020-21 Red Hat Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; see the file COPYING.LIB. If not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1335, USA.
21 * Steve Grubb <sgrubb@redhat.com>
29 #include <stdio_ext.h>
31 #include <sys/prctl.h>
40 #include <pthread.h> // For pthread_atfork
43 #include <sys/syscall.h>
45 #ifdef HAVE_LINUX_SECUREBITS_H
46 #include <linux/securebits.h>
48 #ifdef HAVE_LINUX_MAGIC_H
50 #include <linux/magic.h>
53 # define hidden __attribute__ ((visibility ("hidden")))
54 unsigned int last_cap hidden = 0;
56 * Some milestones of when things became available:
57 * 2.6.24 kernel XATTR_NAME_CAPS
58 * 2.6.25 kernel PR_CAPBSET_DROP, CAPABILITY_VERSION_2
59 * 2.6.26 kernel PR_SET_SECUREBITS, SECURE_*_LOCKED, VERSION_3
60 * 3.5 kernel PR_SET_NO_NEW_PRIVS
61 * 4.3 kernel PR_CAP_AMBIENT
62 * 4.14 kernel VFS_CAP_REVISION_3
64 #ifdef PR_CAPBSET_DROP
65 static int HAVE_PR_CAPBSET_DROP = 0;
67 #ifdef PR_SET_SECUREBITS
68 static int HAVE_PR_SET_SECUREBITS = 0;
70 #ifdef PR_SET_NO_NEW_PRIVS
71 static int HAVE_PR_SET_NO_NEW_PRIVS = 0;
74 static int HAVE_PR_CAP_AMBIENT = 0;
77 /* External syscall prototypes */
78 extern int capset(cap_user_header_t header, cap_user_data_t data);
79 extern int capget(cap_user_header_t header, const cap_user_data_t data);
82 static void update_bounding_set(capng_act_t action, unsigned int capability,
84 static void update_ambient_set(capng_act_t action, unsigned int capability,
88 #define MASK(x) (1U << (x))
89 #ifdef PR_CAPBSET_DROP
90 #define UPPER_MASK ~((~0U)<<(last_cap-31))
92 // For v1 systems UPPER_MASK will never be used
93 #define UPPER_MASK (unsigned)(~0U)
96 // Re-define cap_valid so its uniform between V1 and V3
98 #define cap_valid(x) ((x) <= last_cap)
100 // If we don't have the xattr library, then we can't
101 // compile-in file system capabilities
102 #if !defined(HAVE_ATTR_XATTR_H) && !defined (HAVE_SYS_XATTR_H)
107 #ifdef HAVE_SYS_XATTR_H
108 #include <sys/xattr.h>
110 #ifdef HAVE_ATTR_XATTR_H
111 #include <attr/xattr.h>
114 #if __BYTE_ORDER == __BIG_ENDIAN
115 #define FIXUP(x) bswap_32(x)
121 #ifndef _LINUX_CAPABILITY_VERSION_1
122 #define _LINUX_CAPABILITY_VERSION_1 0x19980330
124 #ifndef _LINUX_CAPABILITY_VERSION_2
125 #define _LINUX_CAPABILITY_VERSION_2 0x20071026
127 #ifndef _LINUX_CAPABILITY_VERSION_3
128 #define _LINUX_CAPABILITY_VERSION_3 0x20080522
131 // This public API went private in the 2.6.36 kernel - hope it never changes
132 #ifndef XATTR_CAPS_SUFFIX
133 #define XATTR_CAPS_SUFFIX "capability"
135 #ifndef XATTR_SECURITY_PREFIX
136 #define XATTR_SECURITY_PREFIX "security."
138 #ifndef XATTR_NAME_CAPS
139 #define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX
143 /* Child processes can't get caps back */
144 #ifndef SECURE_NOROOT
145 #define SECURE_NOROOT 0
147 #ifndef SECURE_NOROOT_LOCKED
148 #define SECURE_NOROOT_LOCKED 1 /* make bit-0 immutable */
150 /* Setuid apps run by uid 0 don't get caps back */
151 #ifndef SECURE_NO_SETUID_FIXUP
152 #define SECURE_NO_SETUID_FIXUP 2
154 #ifndef SECURE_NO_SETUID_FIXUP_LOCKED
155 #define SECURE_NO_SETUID_FIXUP_LOCKED 3 /* make bit-2 immutable */
159 #define VFS_CAP_U32 2
162 #if (VFS_CAP_U32 != 2)
163 #error VFS_CAP_U32 does not match the library, you need a new version
167 // States: new, allocated, initted, updated, applied
168 typedef enum { CAPNG_NEW, CAPNG_ERROR, CAPNG_ALLOCATED, CAPNG_INIT,
169 CAPNG_UPDATED, CAPNG_APPLIED } capng_states_t;
171 // Create an easy data struct out of the kernel definitions
173 struct __user_cap_data_struct v1;
174 struct __user_cap_data_struct v3[VFS_CAP_U32];
177 // This struct keeps all state info
182 struct __user_cap_header_struct hdr;
184 capng_states_t state;
186 __u32 bounds[VFS_CAP_U32];
187 __u32 ambient[VFS_CAP_U32];
190 // Global variables with per thread uniqueness
191 static __thread struct cap_ng m = { 1, 1,
194 CAPNG_NEW, CAPNG_UNSET_ROOTID,
199 * Reset the state so that init gets called to erase everything
201 static void deinit(void)
206 static inline int test_cap(unsigned int cap)
208 // prctl returns 0 or 1 for valid caps, -1 otherwise
209 return prctl(PR_CAPBSET_READ, cap) >= 0;
212 // The maximum cap value is determined by VFS_CAP_U32
213 #define MAX_CAP_VALUE (VFS_CAP_U32 * sizeof(__le32) * 8)
215 static void init_lib(void) __attribute__ ((constructor));
216 static void init_lib(void)
218 // This is so dynamic libraries don't re-init
219 static unsigned int run_once = 0;
224 #ifdef HAVE_PTHREAD_H
225 pthread_atfork(NULL, NULL, deinit);
231 // Try to read last cap from procfs
232 fd = open("/proc/sys/kernel/cap_last_cap", O_RDONLY);
234 #ifdef HAVE_LINUX_MAGIC_H
236 // Bail out if procfs is invalid or fstatfs fails
237 if (fstatfs(fd, &st) || st.f_type != PROC_SUPER_MAGIC)
241 int num = read(fd, buf, sizeof(buf) - 1);
245 unsigned int val = strtoul(buf, NULL, 10);
252 // Run a binary search over capabilities
254 // starting with last_cap=MAX_CAP_VALUE means we always know
255 // that cap1 is invalid after the first iteration
256 last_cap = MAX_CAP_VALUE;
257 unsigned int cap0 = 0, cap1 = MAX_CAP_VALUE;
259 while (cap0 < last_cap) {
260 if (test_cap(last_cap))
265 last_cap = (cap0 + cap1) / 2U;
269 // Detect prctl options at runtime
270 #ifdef PR_CAPBSET_DROP
272 prctl(PR_CAPBSET_READ, 0, 0, 0, 0);
274 HAVE_PR_CAPBSET_DROP = 1;
276 #ifdef PR_SET_SECUREBITS
278 prctl(PR_GET_SECUREBITS, 0, 0, 0, 0);
280 HAVE_PR_SET_SECUREBITS = 1;
282 #ifdef PR_SET_NO_NEW_PRIVS
284 prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0);
286 HAVE_PR_SET_NO_NEW_PRIVS = 1;
288 #ifdef PR_CAP_AMBIENT
290 prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, 0, 0, 0);
292 HAVE_PR_CAP_AMBIENT = 1;
296 static void init(void)
298 // This is so static libs get initialized
301 if (m.state != CAPNG_NEW)
304 memset(&m.hdr, 0, sizeof(m.hdr));
305 (void)capget(&m.hdr, NULL); // Returns -EINVAL
306 if (m.hdr.version == _LINUX_CAPABILITY_VERSION_3 ||
307 m.hdr.version == _LINUX_CAPABILITY_VERSION_2) {
309 } else if (m.hdr.version == _LINUX_CAPABILITY_VERSION_1) {
312 m.state = CAPNG_ERROR;
316 #if VFS_CAP_REVISION == VFS_CAP_REVISION_1
319 m.vfs_cap_ver = 2; // Intentionally set to 2 for both 2 & 3
322 memset(&m.data, 0, sizeof(cap_data_t));
323 #ifdef HAVE_SYSCALL_H
324 m.hdr.pid = (unsigned)syscall(__NR_gettid);
326 m.hdr.pid = (unsigned)getpid();
328 m.rootid = CAPNG_UNSET_ROOTID;
329 m.state = CAPNG_ALLOCATED;
332 void capng_clear(capng_select_t set)
334 if (m.state == CAPNG_NEW)
336 if (m.state == CAPNG_ERROR)
339 if (set & CAPNG_SELECT_CAPS)
340 memset(&m.data, 0, sizeof(cap_data_t));
341 #ifdef PR_CAPBSET_DROP
342 if (HAVE_PR_CAPBSET_DROP) {
343 if (set & CAPNG_SELECT_BOUNDS)
344 memset(m.bounds, 0, sizeof(m.bounds));
347 #ifdef PR_CAP_AMBIENT
348 if (HAVE_PR_CAP_AMBIENT) {
349 if (set & CAPNG_SELECT_AMBIENT)
350 memset(m.ambient, 0, sizeof(m.ambient));
353 m.state = CAPNG_INIT;
356 void capng_fill(capng_select_t set)
358 if (m.state == CAPNG_NEW)
360 if (m.state == CAPNG_ERROR)
363 if (set & CAPNG_SELECT_CAPS) {
364 if (m.cap_ver == 1) {
365 m.data.v1.effective = 0x7FFFFFFFU;
366 m.data.v1.permitted = 0x7FFFFFFFU;
367 m.data.v1.inheritable = 0;
369 m.data.v3[0].effective = 0xFFFFFFFFU;
370 m.data.v3[0].permitted = 0xFFFFFFFFU;
371 m.data.v3[0].inheritable = 0;
372 m.data.v3[1].effective = 0xFFFFFFFFU;
373 m.data.v3[1].permitted = 0xFFFFFFFFU;
374 m.data.v3[1].inheritable = 0;
377 #ifdef PR_CAPBSET_DROP
378 if (HAVE_PR_CAPBSET_DROP) {
379 if (set & CAPNG_SELECT_BOUNDS) {
381 for (i=0; i<sizeof(m.bounds)/sizeof(__u32); i++)
382 m.bounds[i] = 0xFFFFFFFFU;
386 #ifdef PR_CAP_AMBIENT
387 if (HAVE_PR_CAP_AMBIENT) {
388 if (set & CAPNG_SELECT_AMBIENT) {
390 for (i=0; i<sizeof(m.ambient)/sizeof(__u32); i++)
391 m.ambient[i] = 0xFFFFFFFFU;
395 m.state = CAPNG_INIT;
398 void capng_setpid(int pid)
400 if (m.state == CAPNG_NEW)
402 if (m.state == CAPNG_ERROR)
408 int capng_get_rootid(void)
410 #ifdef VFS_CAP_REVISION_3
413 return CAPNG_UNSET_ROOTID;
417 int capng_set_rootid(int rootid)
419 #ifdef VFS_CAP_REVISION_3
420 if (m.state == CAPNG_NEW)
422 if (m.state == CAPNG_ERROR)
437 #ifdef PR_CAPBSET_DROP
438 static int get_bounding_set(void)
444 snprintf(buf, sizeof(buf), "/proc/%d/status", m.hdr.pid ? m.hdr.pid :
445 #ifdef HAVE_SYSCALL_H
446 (int)syscall(__NR_gettid));
450 f = fopen(buf, "re");
452 __fsetlocking(f, FSETLOCKING_BYCALLER);
453 while (fgets(buf, sizeof(buf), f)) {
454 if (strncmp(buf, "CapB", 4))
456 sscanf(buf, "CapBnd: %08x%08x",
457 &m.bounds[1], &m.bounds[0]);
461 // Didn't find bounding set, fall through and try prctl way
464 // Might be in a container with no procfs - do it the hard way
465 memset(m.bounds, 0, sizeof(m.bounds));
468 rc = prctl(PR_CAPBSET_READ, i, 0, 0, 0);
474 update_bounding_set(CAPNG_ADD, i%32, i>>5);
476 } while (cap_valid(i));
482 #ifdef PR_CAP_AMBIENT
483 static int get_ambient_set(void)
489 snprintf(buf, sizeof(buf), "/proc/%d/status", m.hdr.pid ? m.hdr.pid :
490 #ifdef HAVE_SYSCALL_H
491 (int)syscall(__NR_gettid));
495 f = fopen(buf, "re");
497 __fsetlocking(f, FSETLOCKING_BYCALLER);
498 while (fgets(buf, sizeof(buf), f)) {
499 if (strncmp(buf, "CapA", 4))
501 sscanf(buf, "CapAmb: %08x%08x",
502 &m.ambient[1], &m.ambient[0]);
507 // Didn't find ambient set, fall through and try prctl way
509 // Might be in a container with no procfs - do it the hard way
510 memset(m.ambient, 0, sizeof(m.ambient));
513 rc = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, i, 0, 0);
519 update_ambient_set(CAPNG_ADD, i%32, i>>5);
521 } while (cap_valid(i));
528 * Returns 0 on success and -1 on failure
530 int capng_get_caps_process(void)
534 if (m.state == CAPNG_NEW)
536 if (m.state == CAPNG_ERROR)
539 rc = capget((cap_user_header_t)&m.hdr, (cap_user_data_t)&m.data);
541 m.state = CAPNG_INIT;
542 #ifdef PR_CAPBSET_DROP
543 if (HAVE_PR_CAPBSET_DROP) {
544 rc = get_bounding_set();
546 m.state = CAPNG_ERROR;
549 #ifdef PR_CAP_AMBIENT
550 if (HAVE_PR_CAP_AMBIENT) {
551 rc = get_ambient_set();
553 m.state = CAPNG_ERROR;
562 #ifdef VFS_CAP_REVISION_3
563 static int load_data(const struct vfs_ns_cap_data *filedata, int size)
565 static int load_data(const struct vfs_cap_data *filedata, int size)
571 return -1; // Should never get here but just in case
573 magic = FIXUP(filedata->magic_etc);
574 switch (magic & VFS_CAP_REVISION_MASK)
576 case VFS_CAP_REVISION_1:
578 if (size != XATTR_CAPS_SZ_1)
581 case VFS_CAP_REVISION_2:
583 if (size != XATTR_CAPS_SZ_2)
586 #ifdef VFS_CAP_REVISION_3
587 case VFS_CAP_REVISION_3:
589 if (size != XATTR_CAPS_SZ_3)
597 // Now stuff the data structures
598 m.data.v3[0].permitted = FIXUP(filedata->data[0].permitted);
599 m.data.v3[1].permitted = FIXUP(filedata->data[1].permitted);
600 m.data.v3[0].inheritable = FIXUP(filedata->data[0].inheritable);
601 m.data.v3[1].inheritable = FIXUP(filedata->data[1].inheritable);
602 if (magic & VFS_CAP_FLAGS_EFFECTIVE) {
603 m.data.v3[0].effective =
604 m.data.v3[0].permitted | m.data.v3[0].inheritable;
605 m.data.v3[1].effective =
606 m.data.v3[1].permitted | m.data.v3[1].inheritable;
608 m.data.v3[0].effective = 0;
609 m.data.v3[1].effective = 0;
611 #ifdef VFS_CAP_REVISION_3
612 if (size == XATTR_CAPS_SZ_3) {
613 struct vfs_ns_cap_data *d = (struct vfs_ns_cap_data *)filedata;
614 m.rootid = FIXUP(d->rootid);
621 int capng_get_caps_fd(int fd)
627 #ifdef VFS_CAP_REVISION_3
628 struct vfs_ns_cap_data filedata;
630 struct vfs_cap_data filedata;
632 if (m.state == CAPNG_NEW)
634 if (m.state == CAPNG_ERROR)
637 rc = fgetxattr(fd, XATTR_NAME_CAPS, &filedata, sizeof(filedata));
641 rc = load_data(&filedata, rc);
643 m.state = CAPNG_INIT;
645 m.state = CAPNG_ERROR; // If load data failed, malformed data
651 static void v1_update(capng_act_t action, unsigned int capability, __u32 *data)
653 if (action == CAPNG_ADD)
654 *data |= MASK(capability);
656 *data &= ~(MASK(capability));
659 static void update_effective(capng_act_t action, unsigned int capability,
662 if (action == CAPNG_ADD)
663 m.data.v3[idx].effective |= MASK(capability);
665 m.data.v3[idx].effective &= ~(MASK(capability));
668 static void update_permitted(capng_act_t action, unsigned int capability,
671 if (action == CAPNG_ADD)
672 m.data.v3[idx].permitted |= MASK(capability);
674 m.data.v3[idx].permitted &= ~(MASK(capability));
677 static void update_inheritable(capng_act_t action, unsigned int capability,
680 if (action == CAPNG_ADD)
681 m.data.v3[idx].inheritable |= MASK(capability);
683 m.data.v3[idx].inheritable &= ~(MASK(capability));
686 static void update_bounding_set(capng_act_t action, unsigned int capability,
689 #ifdef PR_CAPBSET_DROP
690 if (HAVE_PR_CAPBSET_DROP) {
691 if (action == CAPNG_ADD)
692 m.bounds[idx] |= MASK(capability);
694 m.bounds[idx] &= ~(MASK(capability));
699 static void update_ambient_set(capng_act_t action, unsigned int capability,
702 #ifdef PR_CAP_AMBIENT
703 if (HAVE_PR_CAP_AMBIENT) {
704 if (action == CAPNG_ADD)
705 m.ambient[idx] |= MASK(capability);
707 m.ambient[idx] &= ~(MASK(capability));
712 int capng_update(capng_act_t action, capng_type_t type, unsigned int capability)
714 // Before updating, we expect that the data is initialized to something
715 if (m.state < CAPNG_INIT)
717 if (!cap_valid(capability)) {
722 if (m.cap_ver == 1) {
723 if (CAPNG_EFFECTIVE & type)
724 v1_update(action, capability, &m.data.v1.effective);
725 if (CAPNG_PERMITTED & type)
726 v1_update(action, capability, &m.data.v1.permitted);
727 if (CAPNG_INHERITABLE & type)
728 v1_update(action, capability, &m.data.v1.inheritable);
732 if (capability > 31) {
738 if (CAPNG_EFFECTIVE & type)
739 update_effective(action, capability, idx);
740 if (CAPNG_PERMITTED & type)
741 update_permitted(action, capability, idx);
742 if (CAPNG_INHERITABLE & type)
743 update_inheritable(action, capability, idx);
744 if (CAPNG_BOUNDING_SET & type)
745 update_bounding_set(action, capability, idx);
746 if (CAPNG_AMBIENT & type)
747 update_ambient_set(action, capability, idx);
750 m.state = CAPNG_UPDATED;
754 int capng_updatev(capng_act_t action, capng_type_t type,
755 unsigned int capability, ...)
761 rc = capng_update(action, type, capability);
764 va_start(ap, capability);
765 cap = va_arg(ap, unsigned int);
766 while (cap_valid(cap)) {
767 rc = capng_update(action, type, cap);
770 cap = va_arg(ap, unsigned int);
774 // See if planned exit or invalid
775 if (cap == (unsigned)-1)
785 int capng_apply(capng_select_t set)
789 // Before updating, we expect that the data is initialized to something
790 if (m.state < CAPNG_INIT)
793 if (set & CAPNG_SELECT_BOUNDS) {
794 #ifdef PR_CAPBSET_DROP
795 if (HAVE_PR_CAPBSET_DROP) {
797 memcpy(&state, &m, sizeof(state)); /* save state */
798 if (capng_get_caps_process())
800 if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETPCAP)) {
802 memcpy(&m, &state, sizeof(m)); /* restore state */
803 for (i=0; i <= last_cap; i++) {
804 if (capng_have_capability(CAPNG_BOUNDING_SET,
806 if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) <0) {
812 m.state = CAPNG_APPLIED;
813 if (get_bounding_set() < 0) {
818 memcpy(&m, &state, sizeof(m)); /* restore state */
826 // Try caps is here so that if someone had SELECT_BOTH and we blew up
827 // doing the bounding set, we at least try to set any capabilities
828 // before returning in case the caller also doesn't bother checking
831 if (set & CAPNG_SELECT_CAPS) {
832 if (capset((cap_user_header_t)&m.hdr,
833 (cap_user_data_t)&m.data) == 0)
834 m.state = CAPNG_APPLIED;
839 // Most programs do not and should not mess with ambient capabilities.
840 // Instead of returning here if rc is set, we'll let it try to
841 // do something with ambient capabilities in hopes that it's lowering
842 // capabilities. Again, this is for people that don't check their
845 // Do ambient last so that inheritable and permitted are set by the
847 if (set & CAPNG_SELECT_AMBIENT) {
848 #ifdef PR_CAP_AMBIENT
849 if (HAVE_PR_CAP_AMBIENT) {
850 if (capng_have_capabilities(CAPNG_SELECT_AMBIENT) ==
852 if (prctl(PR_CAP_AMBIENT,
853 PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) < 0) {
861 if (prctl(PR_CAP_AMBIENT,
862 PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) < 0) {
866 for (i=0; i <= last_cap; i++) {
867 if (capng_have_capability(CAPNG_AMBIENT, i))
868 if (prctl(PR_CAP_AMBIENT,
869 PR_CAP_AMBIENT_RAISE, i, 0, 0) < 0){
875 m.state = CAPNG_APPLIED;
884 #ifdef VFS_CAP_REVISION_3
885 static int save_data(struct vfs_ns_cap_data *filedata, int *size)
887 static int save_data(struct vfs_cap_data *filedata, int *size)
890 // Now stuff the data structures
891 if (m.vfs_cap_ver == 1) {
892 filedata->data[0].permitted = FIXUP(m.data.v1.permitted);
893 filedata->data[0].inheritable = FIXUP(m.data.v1.inheritable);
894 filedata->magic_etc = FIXUP(VFS_CAP_REVISION_1);
895 *size = XATTR_CAPS_SZ_1;
896 } else if (m.vfs_cap_ver == 2 || m.vfs_cap_ver == 3) {
899 if (m.data.v3[0].effective || m.data.v3[1].effective)
900 eff = VFS_CAP_FLAGS_EFFECTIVE;
903 filedata->data[0].permitted = FIXUP(m.data.v3[0].permitted);
904 filedata->data[0].inheritable = FIXUP(m.data.v3[0].inheritable);
905 filedata->data[1].permitted = FIXUP(m.data.v3[1].permitted);
906 filedata->data[1].inheritable = FIXUP(m.data.v3[1].inheritable);
907 filedata->magic_etc = FIXUP(VFS_CAP_REVISION_2 | eff);
908 *size = XATTR_CAPS_SZ_2;
910 #ifdef VFS_CAP_REVISION_3
911 if (m.vfs_cap_ver == 3) {
912 // Kernel doesn't support namespaces with non-0 rootid
915 filedata->rootid = FIXUP(m.rootid);
916 *size = XATTR_CAPS_SZ_3;
924 int capng_apply_caps_fd(int fd)
930 #ifdef VFS_CAP_REVISION_3
931 struct vfs_ns_cap_data filedata;
933 struct vfs_cap_data filedata;
937 // Before updating, we expect that the data is initialized to something
938 if (m.state < CAPNG_INIT)
941 if (fstat(fd, &buf) != 0)
943 if (S_ISLNK(buf.st_mode) || !S_ISREG(buf.st_mode)) {
947 if (capng_have_capabilities(CAPNG_SELECT_CAPS) == CAPNG_NONE)
948 rc = fremovexattr(fd, XATTR_NAME_CAPS);
950 if (save_data(&filedata, &size)) {
951 m.state = CAPNG_ERROR;
955 rc = fsetxattr(fd, XATTR_NAME_CAPS, &filedata, size, 0);
959 m.state = CAPNG_APPLIED;
965 // Change uids keeping/removing only certain capabilities
966 // flag to drop supp groups
967 int capng_change_id(int uid, int gid, capng_flags_t flag)
969 int rc, ret, need_setgid, need_setuid;
971 // Before updating, we expect that the data is initialized to something
972 if (m.state < CAPNG_INIT)
975 // Check the current capabilities
976 #ifdef PR_CAPBSET_DROP
977 if (HAVE_PR_CAPBSET_DROP) {
978 // If newer kernel, we need setpcap to change the bounding set
979 if (capng_have_capability(CAPNG_EFFECTIVE, CAP_SETPCAP) == 0 &&
980 flag & CAPNG_CLEAR_BOUNDING)
981 capng_update(CAPNG_ADD,
982 CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_SETPCAP);
985 if (gid == -1 || capng_have_capability(CAPNG_EFFECTIVE, CAP_SETGID))
989 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
992 if (uid == -1 || capng_have_capability(CAPNG_EFFECTIVE, CAP_SETUID))
996 capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
1000 // Tell system we want to keep caps across uid change
1001 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0))
1004 // Change to the temp capabilities
1005 rc = capng_apply(CAPNG_SELECT_CAPS);
1011 // If we are clearing ambient, only clear since its applied at the end
1012 if (flag & CAPNG_CLEAR_AMBIENT)
1013 capng_clear(CAPNG_SELECT_AMBIENT);
1015 // Clear bounding set if needed while we have CAP_SETPCAP
1016 if (flag & CAPNG_CLEAR_BOUNDING) {
1017 capng_clear(CAPNG_SELECT_BOUNDS);
1018 rc = capng_apply(CAPNG_SELECT_BOUNDS);
1027 rc = setresgid(gid, gid, gid);
1034 // See if we need to init supplemental groups
1035 if ((flag & CAPNG_INIT_SUPP_GRP) && uid != -1) {
1036 struct passwd *pw = getpwuid(uid);
1042 if (initgroups(pw->pw_name, gid)) {
1046 } else if (initgroups(pw->pw_name, pw->pw_gid)) {
1052 // See if we need to unload supplemental groups
1053 if ((flag & CAPNG_DROP_SUPP_GRP) && gid != -1) {
1054 if (setgroups(0, NULL)) {
1062 rc = setresuid(uid, uid, uid);
1069 // Tell it we are done keeping capabilities
1070 rc = prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0);
1074 // Now throw away CAP_SETPCAP so no more changes
1076 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
1079 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
1082 // Now drop setpcap & apply
1083 capng_update(CAPNG_DROP, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
1085 rc = capng_apply(CAPNG_SELECT_CAPS|CAPNG_SELECT_AMBIENT);
1090 m.state = CAPNG_UPDATED;
1094 prctl(PR_SET_KEEPCAPS, 0, 0, 0, 0);
1098 int capng_lock(void)
1100 // If either fail, return -1 since something is not right
1101 #ifdef PR_SET_SECUREBITS
1102 if (HAVE_PR_SET_SECUREBITS) {
1103 int rc = prctl(PR_SET_SECUREBITS,
1104 1 << SECURE_NOROOT |
1105 1 << SECURE_NOROOT_LOCKED |
1106 1 << SECURE_NO_SETUID_FIXUP |
1107 1 << SECURE_NO_SETUID_FIXUP_LOCKED, 0, 0, 0);
1108 #ifdef PR_SET_NO_NEW_PRIVS
1109 if (HAVE_PR_SET_NO_NEW_PRIVS) {
1110 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
1122 // -1 - error, 0 - no caps, 1 partial caps, 2 full caps
1123 capng_results_t capng_have_capabilities(capng_select_t set)
1125 int empty = 0, full = 0;
1127 // First, try to init with current set
1128 if (m.state < CAPNG_INIT) {
1129 if (capng_get_caps_process())
1133 // If we still don't have anything, error out
1134 if (m.state < CAPNG_INIT)
1137 if (set & CAPNG_SELECT_CAPS) {
1138 if (m.cap_ver == 1) {
1139 if (m.data.v1.effective == 0)
1141 // after fill, 30 bits starts from upper to lower
1142 else if (m.data.v1.effective == 0x7FFFFFFFU)
1144 // actual capabilities read from system
1145 else if (m.data.v1.effective == 0xFFFFFEFFU)
1148 return CAPNG_PARTIAL;
1150 if (m.data.v3[0].effective == 0)
1152 else if (m.data.v3[0].effective == 0xFFFFFFFFU)
1155 return CAPNG_PARTIAL;
1156 if ((m.data.v3[1].effective & UPPER_MASK) == 0 && !full)
1158 else if ((m.data.v3[1].effective & UPPER_MASK) ==
1159 UPPER_MASK && !empty)
1162 return CAPNG_PARTIAL;
1165 #ifdef PR_CAPBSET_DROP
1166 if (HAVE_PR_CAPBSET_DROP) {
1167 if (set & CAPNG_SELECT_BOUNDS) {
1168 if (m.bounds[0] == 0)
1170 else if (m.bounds[0] == 0xFFFFFFFFU)
1173 return CAPNG_PARTIAL;
1174 if ((m.bounds[1] & UPPER_MASK) == 0)
1176 else if ((m.bounds[1] & UPPER_MASK) == UPPER_MASK)
1179 return CAPNG_PARTIAL;
1184 #ifdef PR_CAP_AMBIENT
1185 if (HAVE_PR_CAP_AMBIENT) {
1186 if (set & CAPNG_SELECT_AMBIENT) {
1187 if (m.ambient[0] == 0)
1189 else if (m.ambient[0] == 0xFFFFFFFFU)
1192 return CAPNG_PARTIAL;
1193 if ((m.ambient[1] & UPPER_MASK) == 0)
1195 else if ((m.ambient[1] & UPPER_MASK) == UPPER_MASK)
1198 return CAPNG_PARTIAL;
1203 if (empty == 1 && full == 0)
1205 else if (empty == 0 && full == 1)
1208 return CAPNG_PARTIAL;
1211 // -1 - error, 0 - no caps, 1 partial caps, 2 full caps
1212 capng_results_t capng_have_permitted_capabilities(void)
1214 int empty = 0, full = 0;
1216 // First, try to init with current set
1217 if (m.state < CAPNG_INIT) {
1218 if (capng_get_caps_process())
1222 // If we still don't have anything, error out
1223 if (m.state < CAPNG_INIT)
1226 if (m.data.v3[0].permitted == 0)
1228 else if (m.data.v3[0].permitted == 0xFFFFFFFFU)
1231 return CAPNG_PARTIAL;
1233 if ((m.data.v3[1].permitted & UPPER_MASK) == 0 && !full)
1235 else if ((m.data.v3[1].permitted & UPPER_MASK) == UPPER_MASK && !empty)
1238 return CAPNG_PARTIAL;
1240 if (empty == 1 && full == 0)
1242 else if (empty == 0 && full == 1)
1245 return CAPNG_PARTIAL;
1248 static int check_effective(unsigned int capability, unsigned int idx)
1250 return MASK(capability) & m.data.v3[idx].effective ? 1 : 0;
1253 static int check_permitted(unsigned int capability, unsigned int idx)
1255 return MASK(capability) & m.data.v3[idx].permitted ? 1 : 0;
1258 static int check_inheritable(unsigned int capability, unsigned int idx)
1260 return MASK(capability) & m.data.v3[idx].inheritable ? 1 : 0;
1263 static int bounds_bit_check(unsigned int capability, unsigned int idx)
1265 #ifdef PR_CAPBSET_DROP
1266 if (HAVE_PR_CAPBSET_DROP) {
1267 return MASK(capability) & m.bounds[idx] ? 1 : 0;
1273 static int ambient_bit_check(unsigned int capability, unsigned int idx)
1275 #ifdef PR_CAP_AMBIENT
1276 if (HAVE_PR_CAP_AMBIENT) {
1277 return MASK(capability) & m.ambient[idx] ? 1 : 0;
1283 static int v1_check(unsigned int capability, __u32 data)
1285 return MASK(capability) & data ? 1 : 0;
1288 int capng_have_capability(capng_type_t which, unsigned int capability)
1290 // First, try to init with current set
1291 if (m.state < CAPNG_INIT) {
1292 if (capng_get_caps_process())
1296 // If we still don't have anything, error out
1297 if (m.state < CAPNG_INIT)
1299 if (m.cap_ver == 1 && capability > 31)
1301 if (!cap_valid(capability))
1304 if (m.cap_ver == 1) {
1305 if (which == CAPNG_EFFECTIVE)
1306 return v1_check(capability, m.data.v1.effective);
1307 else if (which == CAPNG_PERMITTED)
1308 return v1_check(capability, m.data.v1.permitted);
1309 else if (which == CAPNG_INHERITABLE)
1310 return v1_check(capability, m.data.v1.inheritable);
1314 if (capability > 31) {
1315 idx = capability>>5;
1320 if (which == CAPNG_EFFECTIVE)
1321 return check_effective(capability, idx);
1322 else if (which == CAPNG_PERMITTED)
1323 return check_permitted(capability, idx);
1324 else if (which == CAPNG_INHERITABLE)
1325 return check_inheritable(capability, idx);
1326 else if (which == CAPNG_BOUNDING_SET)
1327 return bounds_bit_check(capability, idx);
1328 else if (which == CAPNG_AMBIENT)
1329 return ambient_bit_check(capability, idx);
1334 char *capng_print_caps_numeric(capng_print_t where, capng_select_t set)
1338 if (m.state < CAPNG_INIT)
1341 if (where == CAPNG_PRINT_STDOUT) {
1342 if (set & CAPNG_SELECT_CAPS) {
1343 if (m.cap_ver == 1) {
1344 printf( "Effective: %08X\n"
1346 "Inheritable: %08X\n",
1347 m.data.v1.effective,
1348 m.data.v1.permitted,
1349 m.data.v1.inheritable);
1351 printf( "Effective: %08X, %08X\n"
1352 "Permitted: %08X, %08X\n"
1353 "Inheritable: %08X, %08X\n",
1354 m.data.v3[1].effective & UPPER_MASK,
1355 m.data.v3[0].effective,
1356 m.data.v3[1].permitted & UPPER_MASK,
1357 m.data.v3[0].permitted,
1358 m.data.v3[1].inheritable & UPPER_MASK,
1359 m.data.v3[0].inheritable);
1362 #ifdef PR_CAPBSET_DROP
1363 if (HAVE_PR_CAPBSET_DROP) {
1364 if (set & CAPNG_SELECT_BOUNDS)
1365 printf("Bounding Set: %08X, %08X\n",
1366 m.bounds[1] & UPPER_MASK, m.bounds[0]);
1369 #ifdef PR_CAP_AMBIENT
1370 if (HAVE_PR_CAP_AMBIENT) {
1371 if (set & CAPNG_SELECT_AMBIENT)
1372 printf("Ambient : %08X, %08X\n",
1373 m.ambient[1] & UPPER_MASK, m.ambient[0]);
1376 } else if (where == CAPNG_PRINT_BUFFER) {
1377 if (set & CAPNG_SELECT_CAPS) {
1378 // Make it big enough for bounding & ambient set, too
1380 if (m.cap_ver == 1) {
1384 "Inheritable: %08X\n",
1385 m.data.v1.effective,
1386 m.data.v1.permitted,
1387 m.data.v1.inheritable);
1390 "Effective: %08X, %08X\n"
1391 "Permitted: %08X, %08X\n"
1392 "Inheritable: %08X, %08X\n",
1393 m.data.v3[1].effective & UPPER_MASK,
1394 m.data.v3[0].effective,
1395 m.data.v3[1].permitted & UPPER_MASK,
1396 m.data.v3[0].permitted,
1397 m.data.v3[1].inheritable & UPPER_MASK,
1398 m.data.v3[0].inheritable);
1401 if (set & CAPNG_SELECT_BOUNDS) {
1402 #ifdef PR_CAPBSET_DROP
1403 if (HAVE_PR_CAPBSET_DROP) {
1405 // If ptr is NULL, we only room for bounding and ambient
1413 s = ptr + strlen(ptr);
1414 snprintf(s, 40, "Bounding Set: %08X, %08X\n",
1415 m.bounds[1] & UPPER_MASK, m.bounds[0]);
1419 if (set & CAPNG_SELECT_AMBIENT) {
1420 #ifdef PR_CAP_AMBIENT
1421 if (HAVE_PR_CAP_AMBIENT) {
1423 // If ptr is NULL, we only room for ambient
1431 s = ptr + strlen(ptr);
1432 snprintf(s, 40, "Ambient Set: %08X, %08X\n",
1433 m.ambient[1] & UPPER_MASK,
1443 char *capng_print_caps_text(capng_print_t where, capng_type_t which)
1445 unsigned int i, once = 0, cnt = 0;
1448 if (m.state < CAPNG_INIT)
1451 for (i=0; i<=last_cap; i++) {
1452 if (capng_have_capability(which, i)) {
1453 const char *n = capng_capability_to_name(i);
1456 if (where == CAPNG_PRINT_STDOUT) {
1462 } else if (where == CAPNG_PRINT_BUFFER) {
1465 ptr = malloc(last_cap*20);
1468 len = sprintf(ptr+cnt, "%s", n);
1471 len = sprintf(ptr+cnt, ", %s", n);
1478 if (where == CAPNG_PRINT_STDOUT)
1481 ptr = strdup("none");
1486 void *capng_save_state(void)
1488 void *ptr = malloc(sizeof(m));
1490 memcpy(ptr, &m, sizeof(m));
1494 void capng_restore_state(void **state)
1499 memcpy(&m, ptr, sizeof(m));