| author | |
| committer | |
| log | 1eaf0691f97decfdee9d73a72869ad63805d159b |
| tree | 58564e4b6df649f051681470ae5ced82f2f85a55 |
| parent | 42c32dbc7b6cb474a1aa6dc524acb7451efeb15e |
| parent | 1078810cef4b346bdcd0ab0cab27dd997e68d206 |
| signature |
std: add prctl and securebits definitions for linux/C6 files changed, 246 insertions(+), 0 deletions(-)
lib/std/c.zig+2| ... | ... | @@ -340,3 +340,5 @@ pub extern "c" fn sync() void; |
| 340 | 340 | pub extern "c" fn syncfs(fd: c_int) c_int; |
| 341 | 341 | pub extern "c" fn fsync(fd: c_int) c_int; |
| 342 | 342 | pub extern "c" fn fdatasync(fd: c_int) c_int; |
| 343 | ||
| 344 | pub extern "c" fn prctl(option: c_int, ...) c_int; |
lib/std/os.zig+39| ... | ... | @@ -5418,3 +5418,42 @@ pub fn fdatasync(fd: fd_t) SyncError!void { |
| 5418 | 5418 | else => |err| return std.os.unexpectedErrno(err), |
| 5419 | 5419 | } |
| 5420 | 5420 | } |
| 5421 | ||
| 5422 | pub const PrctlError = error{ | |
| 5423 | /// Can only occur with PR_SET_SECCOMP/SECCOMP_MODE_FILTER or | |
| 5424 | /// PR_SET_MM/PR_SET_MM_EXE_FILE | |
| 5425 | AccessDenied, | |
| 5426 | /// Can only occur with PR_SET_MM/PR_SET_MM_EXE_FILE | |
| 5427 | InvalidFileDescriptor, | |
| 5428 | InvalidAddress, | |
| 5429 | /// Can only occur with PR_SET_SPECULATION_CTRL, PR_MPX_ENABLE_MANAGEMENT, | |
| 5430 | /// or PR_MPX_DISABLE_MANAGEMENT | |
| 5431 | UnsupportedFeature, | |
| 5432 | /// Can only occur wih PR_SET_FP_MODE | |
| 5433 | OperationNotSupported, | |
| 5434 | PermissionDenied, | |
| 5435 | } || UnexpectedError; | |
| 5436 | ||
| 5437 | pub fn prctl(option: i32, args: anytype) PrctlError!u31 { | |
| 5438 | if (@typeInfo(@TypeOf(args)) != .Struct) | |
| 5439 | @compileError("Expected tuple or struct argument, found " ++ @typeName(@TypeOf(args))); | |
| 5440 | if (args.len > 4) | |
| 5441 | @compileError("prctl takes a maximum of 4 optional arguments"); | |
| 5442 | ||
| 5443 | var buf: [4]usize = undefined; | |
| 5444 | inline for (args) |arg, i| buf[i] = arg; | |
| 5445 | ||
| 5446 | const rc = system.prctl(option, buf[0], buf[1], buf[2], buf[3]); | |
| 5447 | switch (errno(rc)) { | |
| 5448 | 0 => return @intCast(u31, rc), | |
| 5449 | EACCES => return error.AccessDenied, | |
| 5450 | EBADF => return error.InvalidFileDescriptor, | |
| 5451 | EFAULT => return error.InvalidAddress, | |
| 5452 | EINVAL => unreachable, | |
| 5453 | ENODEV, ENXIO => return error.UnsupportedFeature, | |
| 5454 | EOPNOTSUPP => return error.OperationNotSupported, | |
| 5455 | EPERM, EBUSY => return error.PermissionDenied, | |
| 5456 | ERANGE => unreachable, | |
| 5457 | else => |err| return std.os.unexpectedErrno(err), | |
| 5458 | } | |
| 5459 | } |
lib/std/os/bits/linux.zig+2| ... | ... | @@ -25,6 +25,8 @@ pub usingnamespace switch (builtin.arch) { |
| 25 | 25 | }; |
| 26 | 26 | |
| 27 | 27 | pub usingnamespace @import("linux/netlink.zig"); |
| 28 | pub usingnamespace @import("linux/prctl.zig"); | |
| 29 | pub usingnamespace @import("linux/securebits.zig"); | |
| 28 | 30 | |
| 29 | 31 | const is_mips = builtin.arch.isMIPS(); |
| 30 | 32 |
lib/std/os/bits/linux/prctl.zig created+158| ... | ... | @@ -0,0 +1,158 @@ |
| 1 | // SPDX-License-Identifier: MIT | |
| 2 | // Copyright (c) 2015-2020 Zig Contributors | |
| 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. | |
| 4 | // The MIT license requires this copyright notice to be included in all copies | |
| 5 | // and substantial portions of the software. | |
| 6 | ||
| 7 | pub const PR_SET_PDEATHSIG = 1; | |
| 8 | pub const PR_GET_PDEATHSIG = 2; | |
| 9 | ||
| 10 | pub const PR_GET_DUMPABLE = 3; | |
| 11 | pub const PR_SET_DUMPABLE = 4; | |
| 12 | ||
| 13 | pub const PR_GET_UNALIGN = 5; | |
| 14 | pub const PR_SET_UNALIGN = 6; | |
| 15 | pub const PR_UNALIGN_NOPRINT = 1; | |
| 16 | pub const PR_UNALIGN_SIGBUS = 2; | |
| 17 | ||
| 18 | pub const PR_GET_KEEPCAPS = 7; | |
| 19 | pub const PR_SET_KEEPCAPS = 8; | |
| 20 | ||
| 21 | pub const PR_GET_FPEMU = 9; | |
| 22 | pub const PR_SET_FPEMU = 10; | |
| 23 | pub const PR_FPEMU_NOPRINT = 1; | |
| 24 | pub const PR_FPEMU_SIGFPE = 2; | |
| 25 | ||
| 26 | pub const PR_GET_FPEXC = 11; | |
| 27 | pub const PR_SET_FPEXC = 12; | |
| 28 | pub const PR_FP_EXC_SW_ENABLE = 0x80; | |
| 29 | pub const PR_FP_EXC_DIV = 0x010000; | |
| 30 | pub const PR_FP_EXC_OVF = 0x020000; | |
| 31 | pub const PR_FP_EXC_UND = 0x040000; | |
| 32 | pub const PR_FP_EXC_RES = 0x080000; | |
| 33 | pub const PR_FP_EXC_INV = 0x100000; | |
| 34 | pub const PR_FP_EXC_DISABLED = 0; | |
| 35 | pub const PR_FP_EXC_NONRECOV = 1; | |
| 36 | pub const PR_FP_EXC_ASYNC = 2; | |
| 37 | pub const PR_FP_EXC_PRECISE = 3; | |
| 38 | ||
| 39 | pub const PR_GET_TIMING = 13; | |
| 40 | pub const PR_SET_TIMING = 14; | |
| 41 | pub const PR_TIMING_STATISTICAL = 0; | |
| 42 | pub const PR_TIMING_TIMESTAMP = 1; | |
| 43 | ||
| 44 | pub const PR_SET_NAME = 15; | |
| 45 | pub const PR_GET_NAME = 16; | |
| 46 | ||
| 47 | pub const PR_GET_ENDIAN = 19; | |
| 48 | pub const PR_SET_ENDIAN = 20; | |
| 49 | pub const PR_ENDIAN_BIG = 0; | |
| 50 | pub const PR_ENDIAN_LITTLE = 1; | |
| 51 | pub const PR_ENDIAN_PPC_LITTLE = 2; | |
| 52 | ||
| 53 | pub const PR_GET_SECCOMP = 21; | |
| 54 | pub const PR_SET_SECCOMP = 22; | |
| 55 | ||
| 56 | pub const PR_CAPBSET_READ = 23; | |
| 57 | pub const PR_CAPBSET_DROP = 24; | |
| 58 | ||
| 59 | pub const PR_GET_TSC = 25; | |
| 60 | pub const PR_SET_TSC = 26; | |
| 61 | pub const PR_TSC_ENABLE = 1; | |
| 62 | pub const PR_TSC_SIGSEGV = 2; | |
| 63 | ||
| 64 | pub const PR_GET_SECUREBITS = 27; | |
| 65 | pub const PR_SET_SECUREBITS = 28; | |
| 66 | ||
| 67 | pub const PR_SET_TIMERSLACK = 29; | |
| 68 | pub const PR_GET_TIMERSLACK = 30; | |
| 69 | ||
| 70 | pub const PR_TASK_PERF_EVENTS_DISABLE = 31; | |
| 71 | pub const PR_TASK_PERF_EVENTS_ENABLE = 32; | |
| 72 | ||
| 73 | pub const PR_MCE_KILL = 33; | |
| 74 | pub const PR_MCE_KILL_CLEAR = 0; | |
| 75 | pub const PR_MCE_KILL_SET = 1; | |
| 76 | ||
| 77 | pub const PR_MCE_KILL_LATE = 0; | |
| 78 | pub const PR_MCE_KILL_EARLY = 1; | |
| 79 | pub const PR_MCE_KILL_DEFAULT = 2; | |
| 80 | ||
| 81 | pub const PR_MCE_KILL_GET = 34; | |
| 82 | ||
| 83 | pub const PR_SET_MM = 35; | |
| 84 | pub const PR_SET_MM_START_CODE = 1; | |
| 85 | pub const PR_SET_MM_END_CODE = 2; | |
| 86 | pub const PR_SET_MM_START_DATA = 3; | |
| 87 | pub const PR_SET_MM_END_DATA = 4; | |
| 88 | pub const PR_SET_MM_START_STACK = 5; | |
| 89 | pub const PR_SET_MM_START_BRK = 6; | |
| 90 | pub const PR_SET_MM_BRK = 7; | |
| 91 | pub const PR_SET_MM_ARG_START = 8; | |
| 92 | pub const PR_SET_MM_ARG_END = 9; | |
| 93 | pub const PR_SET_MM_ENV_START = 10; | |
| 94 | pub const PR_SET_MM_ENV_END = 11; | |
| 95 | pub const PR_SET_MM_AUXV = 12; | |
| 96 | pub const PR_SET_MM_EXE_FILE = 13; | |
| 97 | pub const PR_SET_MM_MAP = 14; | |
| 98 | pub const PR_SET_MM_MAP_SIZE = 15; | |
| 99 | ||
| 100 | pub const prctl_mm_map = extern struct { | |
| 101 | start_code: u64, | |
| 102 | end_code: u64, | |
| 103 | start_data: u64, | |
| 104 | end_data: u64, | |
| 105 | start_brk: u64, | |
| 106 | brk: u64, | |
| 107 | start_stack: u64, | |
| 108 | arg_start: u64, | |
| 109 | arg_end: u64, | |
| 110 | env_start: u64, | |
| 111 | env_end: u64, | |
| 112 | auxv: *u64, | |
| 113 | auxv_size: u32, | |
| 114 | exe_fd: u32, | |
| 115 | }; | |
| 116 | ||
| 117 | pub const PR_SET_PTRACER = 0x59616d61; | |
| 118 | pub const PR_SET_PTRACER_ANY = std.math.maxInt(c_ulong); | |
| 119 | ||
| 120 | pub const PR_SET_CHILD_SUBREAPER = 36; | |
| 121 | pub const PR_GET_CHILD_SUBREAPER = 37; | |
| 122 | ||
| 123 | pub const PR_SET_NO_NEW_PRIVS = 38; | |
| 124 | pub const PR_GET_NO_NEW_PRIVS = 39; | |
| 125 | ||
| 126 | pub const PR_GET_TID_ADDRESS = 40; | |
| 127 | ||
| 128 | pub const PR_SET_THP_DISABLE = 41; | |
| 129 | pub const PR_GET_THP_DISABLE = 42; | |
| 130 | ||
| 131 | pub const PR_MPX_ENABLE_MANAGEMENT = 43; | |
| 132 | pub const PR_MPX_DISABLE_MANAGEMENT = 44; | |
| 133 | ||
| 134 | pub const PR_SET_FP_MODE = 45; | |
| 135 | pub const PR_GET_FP_MODE = 46; | |
| 136 | pub const PR_FP_MODE_FR = 1 << 0; | |
| 137 | pub const PR_FP_MODE_FRE = 1 << 1; | |
| 138 | ||
| 139 | pub const PR_CAP_AMBIENT = 47; | |
| 140 | pub const PR_CAP_AMBIENT_IS_SET = 1; | |
| 141 | pub const PR_CAP_AMBIENT_RAISE = 2; | |
| 142 | pub const PR_CAP_AMBIENT_LOWER = 3; | |
| 143 | pub const PR_CAP_AMBIENT_CLEAR_ALL = 4; | |
| 144 | ||
| 145 | pub const PR_SVE_SET_VL = 50; | |
| 146 | pub const PR_SVE_SET_VL_ONEXEC = 1 << 18; | |
| 147 | pub const PR_SVE_GET_VL = 51; | |
| 148 | pub const PR_SVE_VL_LEN_MASK = 0xffff; | |
| 149 | pub const PR_SVE_VL_INHERIT = 1 << 17; | |
| 150 | ||
| 151 | pub const PR_GET_SPECULATION_CTRL = 52; | |
| 152 | pub const PR_SET_SPECULATION_CTRL = 53; | |
| 153 | pub const PR_SPEC_STORE_BYPASS = 0; | |
| 154 | pub const PR_SPEC_NOT_AFFECTED = 0; | |
| 155 | pub const PR_SPEC_PRCTL = 1 << 0; | |
| 156 | pub const PR_SPEC_ENABLE = 1 << 1; | |
| 157 | pub const PR_SPEC_DISABLE = 1 << 2; | |
| 158 | pub const PR_SPEC_FORCE_DISABLE = 1 << 3; |
lib/std/os/bits/linux/securebits.zig created+41| ... | ... | @@ -0,0 +1,41 @@ |
| 1 | // SPDX-License-Identifier: MIT | |
| 2 | // Copyright (c) 2015-2020 Zig Contributors | |
| 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. | |
| 4 | // The MIT license requires this copyright notice to be included in all copies | |
| 5 | // and substantial portions of the software. | |
| 6 | ||
| 7 | fn issecure_mask(comptime x: comptime_int) comptime_int { | |
| 8 | return 1 << x; | |
| 9 | } | |
| 10 | ||
| 11 | pub const SECUREBITS_DEFAULT = 0x00000000; | |
| 12 | ||
| 13 | pub const SECURE_NOROOT = 0; | |
| 14 | pub const SECURE_NOROOT_LOCKED = 1; | |
| 15 | ||
| 16 | pub const SECBIT_NOROOT = issecure_mask(SECURE_NOROOT); | |
| 17 | pub const SECBIT_NOROOT_LOCKED = issecure_mask(SECURE_NOROOT_LOCKED); | |
| 18 | ||
| 19 | pub const SECURE_NO_SETUID_FIXUP = 2; | |
| 20 | pub const SECURE_NO_SETUID_FIXUP_LOCKED = 3; | |
| 21 | ||
| 22 | pub const SECBIT_NO_SETUID_FIXUP = issecure_mask(SECURE_NO_SETUID_FIXUP); | |
| 23 | pub const SECBIT_NO_SETUID_FIXUP_LOCKED = issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED); | |
| 24 | ||
| 25 | pub const SECURE_KEEP_CAPS = 4; | |
| 26 | pub const SECURE_KEEP_CAPS_LOCKED = 5; | |
| 27 | ||
| 28 | pub const SECBIT_KEEP_CAPS = issecure_mask(SECURE_KEEP_CAPS); | |
| 29 | pub const SECBIT_KEEP_CAPS_LOCKED = issecure_mask(SECURE_KEEP_CAPS_LOCKED); | |
| 30 | ||
| 31 | pub const SECURE_NO_CAP_AMBIENT_RAISE = 6; | |
| 32 | pub const SECURE_NO_CAP_AMBIENT_RAISE_LOCKED = 7; | |
| 33 | ||
| 34 | pub const SECBIT_NO_CAP_AMBIENT_RAISE = issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE); | |
| 35 | pub const SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED = issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED); | |
| 36 | ||
| 37 | pub const SECURE_ALL_BITS = issecure_mask(SECURE_NOROOT) | | |
| 38 | issecure_mask(SECURE_NO_SETUID_FIXUP) | | |
| 39 | issecure_mask(SECURE_KEEP_CAPS) | | |
| 40 | issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE); | |
| 41 | pub const SECURE_ALL_LOCKS = SECURE_ALL_BITS << 1; |
lib/std/os/linux.zig+4| ... | ... | @@ -1259,6 +1259,10 @@ pub fn fdatasync(fd: fd_t) usize { |
| 1259 | 1259 | return syscall1(.fdatasync, @bitCast(usize, @as(isize, fd))); |
| 1260 | 1260 | } |
| 1261 | 1261 | |
| 1262 | pub fn prctl(option: i32, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize { | |
| 1263 | return syscall5(.prctl, @bitCast(usize, @as(isize, option)), arg2, arg3, arg4, arg5); | |
| 1264 | } | |
| 1265 | ||
| 1262 | 1266 | test "" { |
| 1263 | 1267 | if (builtin.os.tag == .linux) { |
| 1264 | 1268 | _ = @import("linux/test.zig"); |