authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-11 16:33:57-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-09-11 16:33:57-04:00
log1eaf0691f97decfdee9d73a72869ad63805d159b
tree58564e4b6df649f051681470ae5ced82f2f85a55
parent42c32dbc7b6cb474a1aa6dc524acb7451efeb15e
parent1078810cef4b346bdcd0ab0cab27dd997e68d206
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6320 from ifreund/prctl

std: add prctl and securebits definitions for linux/C

6 files changed, 246 insertions(+), 0 deletions(-)

lib/std/c.zig+2
...@@ -340,3 +340,5 @@ pub extern "c" fn sync() void;...@@ -340,3 +340,5 @@ pub extern "c" fn sync() void;
340pub extern "c" fn syncfs(fd: c_int) c_int;340pub extern "c" fn syncfs(fd: c_int) c_int;
341pub extern "c" fn fsync(fd: c_int) c_int;341pub extern "c" fn fsync(fd: c_int) c_int;
342pub extern "c" fn fdatasync(fd: c_int) c_int;342pub extern "c" fn fdatasync(fd: c_int) c_int;
343
344pub 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,3 +5418,42 @@ pub fn fdatasync(fd: fd_t) SyncError!void {
5418 else => |err| return std.os.unexpectedErrno(err),5418 else => |err| return std.os.unexpectedErrno(err),
5419 }5419 }
5420}5420}
5421
5422pub 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
5437pub 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,6 +25,8 @@ pub usingnamespace switch (builtin.arch) {
25};25};
2626
27pub usingnamespace @import("linux/netlink.zig");27pub usingnamespace @import("linux/netlink.zig");
28pub usingnamespace @import("linux/prctl.zig");
29pub usingnamespace @import("linux/securebits.zig");
2830
29const is_mips = builtin.arch.isMIPS();31const is_mips = builtin.arch.isMIPS();
3032
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
7pub const PR_SET_PDEATHSIG = 1;
8pub const PR_GET_PDEATHSIG = 2;
9
10pub const PR_GET_DUMPABLE = 3;
11pub const PR_SET_DUMPABLE = 4;
12
13pub const PR_GET_UNALIGN = 5;
14pub const PR_SET_UNALIGN = 6;
15pub const PR_UNALIGN_NOPRINT = 1;
16pub const PR_UNALIGN_SIGBUS = 2;
17
18pub const PR_GET_KEEPCAPS = 7;
19pub const PR_SET_KEEPCAPS = 8;
20
21pub const PR_GET_FPEMU = 9;
22pub const PR_SET_FPEMU = 10;
23pub const PR_FPEMU_NOPRINT = 1;
24pub const PR_FPEMU_SIGFPE = 2;
25
26pub const PR_GET_FPEXC = 11;
27pub const PR_SET_FPEXC = 12;
28pub const PR_FP_EXC_SW_ENABLE = 0x80;
29pub const PR_FP_EXC_DIV = 0x010000;
30pub const PR_FP_EXC_OVF = 0x020000;
31pub const PR_FP_EXC_UND = 0x040000;
32pub const PR_FP_EXC_RES = 0x080000;
33pub const PR_FP_EXC_INV = 0x100000;
34pub const PR_FP_EXC_DISABLED = 0;
35pub const PR_FP_EXC_NONRECOV = 1;
36pub const PR_FP_EXC_ASYNC = 2;
37pub const PR_FP_EXC_PRECISE = 3;
38
39pub const PR_GET_TIMING = 13;
40pub const PR_SET_TIMING = 14;
41pub const PR_TIMING_STATISTICAL = 0;
42pub const PR_TIMING_TIMESTAMP = 1;
43
44pub const PR_SET_NAME = 15;
45pub const PR_GET_NAME = 16;
46
47pub const PR_GET_ENDIAN = 19;
48pub const PR_SET_ENDIAN = 20;
49pub const PR_ENDIAN_BIG = 0;
50pub const PR_ENDIAN_LITTLE = 1;
51pub const PR_ENDIAN_PPC_LITTLE = 2;
52
53pub const PR_GET_SECCOMP = 21;
54pub const PR_SET_SECCOMP = 22;
55
56pub const PR_CAPBSET_READ = 23;
57pub const PR_CAPBSET_DROP = 24;
58
59pub const PR_GET_TSC = 25;
60pub const PR_SET_TSC = 26;
61pub const PR_TSC_ENABLE = 1;
62pub const PR_TSC_SIGSEGV = 2;
63
64pub const PR_GET_SECUREBITS = 27;
65pub const PR_SET_SECUREBITS = 28;
66
67pub const PR_SET_TIMERSLACK = 29;
68pub const PR_GET_TIMERSLACK = 30;
69
70pub const PR_TASK_PERF_EVENTS_DISABLE = 31;
71pub const PR_TASK_PERF_EVENTS_ENABLE = 32;
72
73pub const PR_MCE_KILL = 33;
74pub const PR_MCE_KILL_CLEAR = 0;
75pub const PR_MCE_KILL_SET = 1;
76
77pub const PR_MCE_KILL_LATE = 0;
78pub const PR_MCE_KILL_EARLY = 1;
79pub const PR_MCE_KILL_DEFAULT = 2;
80
81pub const PR_MCE_KILL_GET = 34;
82
83pub const PR_SET_MM = 35;
84pub const PR_SET_MM_START_CODE = 1;
85pub const PR_SET_MM_END_CODE = 2;
86pub const PR_SET_MM_START_DATA = 3;
87pub const PR_SET_MM_END_DATA = 4;
88pub const PR_SET_MM_START_STACK = 5;
89pub const PR_SET_MM_START_BRK = 6;
90pub const PR_SET_MM_BRK = 7;
91pub const PR_SET_MM_ARG_START = 8;
92pub const PR_SET_MM_ARG_END = 9;
93pub const PR_SET_MM_ENV_START = 10;
94pub const PR_SET_MM_ENV_END = 11;
95pub const PR_SET_MM_AUXV = 12;
96pub const PR_SET_MM_EXE_FILE = 13;
97pub const PR_SET_MM_MAP = 14;
98pub const PR_SET_MM_MAP_SIZE = 15;
99
100pub 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
117pub const PR_SET_PTRACER = 0x59616d61;
118pub const PR_SET_PTRACER_ANY = std.math.maxInt(c_ulong);
119
120pub const PR_SET_CHILD_SUBREAPER = 36;
121pub const PR_GET_CHILD_SUBREAPER = 37;
122
123pub const PR_SET_NO_NEW_PRIVS = 38;
124pub const PR_GET_NO_NEW_PRIVS = 39;
125
126pub const PR_GET_TID_ADDRESS = 40;
127
128pub const PR_SET_THP_DISABLE = 41;
129pub const PR_GET_THP_DISABLE = 42;
130
131pub const PR_MPX_ENABLE_MANAGEMENT = 43;
132pub const PR_MPX_DISABLE_MANAGEMENT = 44;
133
134pub const PR_SET_FP_MODE = 45;
135pub const PR_GET_FP_MODE = 46;
136pub const PR_FP_MODE_FR = 1 << 0;
137pub const PR_FP_MODE_FRE = 1 << 1;
138
139pub const PR_CAP_AMBIENT = 47;
140pub const PR_CAP_AMBIENT_IS_SET = 1;
141pub const PR_CAP_AMBIENT_RAISE = 2;
142pub const PR_CAP_AMBIENT_LOWER = 3;
143pub const PR_CAP_AMBIENT_CLEAR_ALL = 4;
144
145pub const PR_SVE_SET_VL = 50;
146pub const PR_SVE_SET_VL_ONEXEC = 1 << 18;
147pub const PR_SVE_GET_VL = 51;
148pub const PR_SVE_VL_LEN_MASK = 0xffff;
149pub const PR_SVE_VL_INHERIT = 1 << 17;
150
151pub const PR_GET_SPECULATION_CTRL = 52;
152pub const PR_SET_SPECULATION_CTRL = 53;
153pub const PR_SPEC_STORE_BYPASS = 0;
154pub const PR_SPEC_NOT_AFFECTED = 0;
155pub const PR_SPEC_PRCTL = 1 << 0;
156pub const PR_SPEC_ENABLE = 1 << 1;
157pub const PR_SPEC_DISABLE = 1 << 2;
158pub 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
7fn issecure_mask(comptime x: comptime_int) comptime_int {
8 return 1 << x;
9}
10
11pub const SECUREBITS_DEFAULT = 0x00000000;
12
13pub const SECURE_NOROOT = 0;
14pub const SECURE_NOROOT_LOCKED = 1;
15
16pub const SECBIT_NOROOT = issecure_mask(SECURE_NOROOT);
17pub const SECBIT_NOROOT_LOCKED = issecure_mask(SECURE_NOROOT_LOCKED);
18
19pub const SECURE_NO_SETUID_FIXUP = 2;
20pub const SECURE_NO_SETUID_FIXUP_LOCKED = 3;
21
22pub const SECBIT_NO_SETUID_FIXUP = issecure_mask(SECURE_NO_SETUID_FIXUP);
23pub const SECBIT_NO_SETUID_FIXUP_LOCKED = issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED);
24
25pub const SECURE_KEEP_CAPS = 4;
26pub const SECURE_KEEP_CAPS_LOCKED = 5;
27
28pub const SECBIT_KEEP_CAPS = issecure_mask(SECURE_KEEP_CAPS);
29pub const SECBIT_KEEP_CAPS_LOCKED = issecure_mask(SECURE_KEEP_CAPS_LOCKED);
30
31pub const SECURE_NO_CAP_AMBIENT_RAISE = 6;
32pub const SECURE_NO_CAP_AMBIENT_RAISE_LOCKED = 7;
33
34pub const SECBIT_NO_CAP_AMBIENT_RAISE = issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE);
35pub const SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED = issecure_mask(SECURE_NO_CAP_AMBIENT_RAISE_LOCKED);
36
37pub 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);
41pub 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,6 +1259,10 @@ pub fn fdatasync(fd: fd_t) usize {
1259 return syscall1(.fdatasync, @bitCast(usize, @as(isize, fd)));1259 return syscall1(.fdatasync, @bitCast(usize, @as(isize, fd)));
1260}1260}
12611261
1262pub 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
1262test "" {1266test "" {
1263 if (builtin.os.tag == .linux) {1267 if (builtin.os.tag == .linux) {
1264 _ = @import("linux/test.zig");1268 _ = @import("linux/test.zig");