chiark / gitweb /
shared/capability: go frugal on space for caps
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 1 Feb 2015 20:26:46 +0000 (15:26 -0500)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 1 Feb 2015 22:22:32 +0000 (17:22 -0500)
src/shared/capability.c
src/shared/util.h

index dcf7ce1..3f2f27e 100644 (file)
@@ -269,11 +269,12 @@ int drop_privileges(uid_t uid, gid_t gid, uint64_t keep_capabilities) {
                 return log_oom();
 
         if (keep_capabilities) {
                 return log_oom();
 
         if (keep_capabilities) {
-                cap_value_t bits[sizeof(keep_capabilities)*8];
+                cap_value_t bits[log2u64(keep_capabilities)];
 
 
-                for (i = 0; i < sizeof(keep_capabilities)*8; i++)
+                for (i = 0; i < ELEMENTSOF(bits); i++)
                         if (keep_capabilities & (1ULL << i))
                                 bits[j++] = i;
                         if (keep_capabilities & (1ULL << i))
                                 bits[j++] = i;
+                assert((keep_capabilities & (~1ULL << i)) == 0);
 
                 if (cap_set_flag(d, CAP_EFFECTIVE, j, bits, CAP_SET) < 0 ||
                     cap_set_flag(d, CAP_PERMITTED, j, bits, CAP_SET) < 0) {
 
                 if (cap_set_flag(d, CAP_EFFECTIVE, j, bits, CAP_SET) < 0 ||
                     cap_set_flag(d, CAP_PERMITTED, j, bits, CAP_SET) < 0) {
index 5312422..410ce65 100644 (file)
@@ -860,7 +860,7 @@ static inline unsigned u32ctz(uint32_t n) {
 #endif
 }
 
 #endif
 }
 
-static inline int log2i(int x) {
+static inline unsigned log2i(int x) {
         assert(x > 0);
 
         return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
         assert(x > 0);
 
         return __SIZEOF_INT__ * 8 - __builtin_clz(x) - 1;
@@ -872,6 +872,16 @@ static inline unsigned log2u(unsigned x) {
         return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
 }
 
         return sizeof(unsigned) * 8 - __builtin_clz(x) - 1;
 }
 
+static inline unsigned log2u64(uint64_t x) {
+        assert(x > 0);
+
+#if __SIZEOF_LONG_LONG__ == 8
+        return 64 - __builtin_clzll(x) - 1;
+#else
+#error "Wut?"
+#endif
+}
+
 static inline unsigned log2u_round_up(unsigned x) {
         assert(x > 0);
 
 static inline unsigned log2u_round_up(unsigned x) {
         assert(x > 0);