| 1 | // Copyright 2018 psgo authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | // Package capabilities provides a mapping from common kernel bit masks to the |
| 16 | // alphanumerical representation of kernel capabilities. See capabilities(7) |
| 17 | // for additional information. |
| 18 | package capabilities |
| 19 | |
| 20 | var ( |
| 21 | // capabilities are a mapping from a numerical value to the textual |
| 22 | // representation of a given capability. A map allows to easily check |
| 23 | // if a given value is included or not. |
| 24 | // |
| 25 | // NOTE: this map must be maintained and kept in sync with the |
| 26 | // ./include/uapi/linux/capability.h kernel header. |
| 27 | capabilities = map[uint]string{ |
| 28 | 0: "CHOWN", |
| 29 | 1: "DAC_OVERRIDE", |
| 30 | 2: "DAC_READ_SEARCH", |
| 31 | 3: "FOWNER", |
| 32 | 4: "FSETID", |
| 33 | 5: "KILL", |
| 34 | 6: "SETGID", |
| 35 | 7: "SETUID", |
| 36 | 8: "SETPCAP", |
| 37 | 9: "LINUX_IMMUTABLE", |
| 38 | 10: "NET_BIND_SERVICE", |
| 39 | 11: "NET_BROADCAST", |
| 40 | 12: "NET_ADMIN", |
| 41 | 13: "NET_RAW", |
| 42 | 14: "IPC_LOCK", |
| 43 | 15: "IPC_OWNER", |
| 44 | 16: "SYS_MODULE", |
| 45 | 17: "SYS_RAWIO", |
| 46 | 18: "SYS_CHROOT", |
| 47 | 19: "SYS_PTRACE", |
| 48 | 20: "SYS_PACCT", |
| 49 | 21: "SYS_ADMIN", |
| 50 | 22: "SYS_BOOT", |
| 51 | 23: "SYS_NICE", |
| 52 | 24: "SYS_RESOURCE", |
| 53 | 25: "SYS_TIME", |
| 54 | 26: "SYS_TTY_CONFIG", |
| 55 | 27: "MKNOD", |
| 56 | 28: "LEASE", |
| 57 | 29: "AUDIT_WRITE", |
| 58 | 30: "AUDIT_CONTROL", |
| 59 | 31: "SETFCAP", |
| 60 | 32: "MAC_OVERRIDE", |
| 61 | 33: "MAC_ADMIN", |
| 62 | 34: "SYSLOG", |
| 63 | 35: "WAKE_ALARM", |
| 64 | 36: "BLOCK_SUSPEND", |
| 65 | 37: "AUDIT_READ", |
| 66 | 38: "PERFMON", |
| 67 | 39: "BPF", |
| 68 | 40: "CHECKPOINT_RESTORE", |
| 69 | } |
| 70 | |
| 71 | // FullCAPs represents the value of a bitmask with a full capability |
| 72 | // set. |
| 73 | FullCAPs = uint64(0x1FFFFFFFFFF) |
| 74 | ) |
| 75 | |
| 76 | // TranslateMask iterates over mask and returns a slice of corresponding |
| 77 | // capabilities. If a bit is out of range of known capabilities, it is set as |
| 78 | // "unknown" to catch potential regressions when new capabilities are added to |
| 79 | // the kernel. |
| 80 | func TranslateMask(mask uint64) []string { |
| 81 | caps := []string{} |
| 82 | for i := range uint(64) { |
| 83 | if (mask>>i)&0x1 == 1 { |
| 84 | c, known := capabilities[i] |
| 85 | if !known { |
| 86 | c = "unknown" |
| 87 | } |
| 88 | caps = append(caps, c) |
| 89 | } |
| 90 | } |
| 91 | return caps |
| 92 | } |