Skip to content

Commit 96de564

Browse files
committed
new(driver,userspace): PT_MODES for file modes
Signed-off-by: Lorenzo Fontana <[email protected]>
1 parent 5de3f25 commit 96de564

17 files changed

+188
-12
lines changed

driver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ set(DRIVER_SOURCES
4545
event_table.c
4646
fillers_table.c
4747
flags_table.c
48+
modes_table.c
4849
main.c
4950
ppm.h
5051
ppm_events.c

driver/bpf/filler_helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ static __always_inline int __bpf_val_to_ring(struct filler_data *data,
830830
len = sizeof(u16);
831831
break;
832832
case PT_FLAGS32:
833+
case PT_MODES:
833834
case PT_UINT32:
834835
case PT_UID:
835836
case PT_GID:

driver/bpf/fillers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4183,6 +4183,7 @@ FILLER(sys_fchmodat_x, true)
41834183
unsigned long val;
41844184
int res;
41854185
long retval;
4186+
unsigned int mode;
41864187

41874188
retval = bpf_syscall_get_retval(data->ctx);
41884189
res = bpf_val_to_ring(data, retval);
@@ -4211,8 +4212,9 @@ FILLER(sys_fchmodat_x, true)
42114212
/*
42124213
* mode
42134214
*/
4214-
val = bpf_syscall_get_argument(data, 2);
4215-
res = bpf_val_to_ring(data, val);
4215+
mode = bpf_syscall_get_argument(data, 2);
4216+
mode = chmod_modes_to_scap(mode);
4217+
res = bpf_val_to_ring(data, mode);
42164218

42174219
return res;
42184220
}

driver/event_table.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ const struct ppm_event_info g_event_info[PPM_EVENT_MAX] = {
325325
/* PPME_SYSCALL_LINKAT_2_E */{"linkat", EC_FILE, EF_NONE, 0},
326326
/* PPME_SYSCALL_LINKAT_2_X */{"linkat", EC_FILE, EF_NONE, 6, {{"res", PT_ERRNO, PF_DEC}, {"olddir", PT_FD, PF_DEC}, {"oldpath", PT_CHARBUF, PF_NA}, {"newdir", PT_FD, PF_DEC}, {"newpath", PT_CHARBUF, PF_NA}, {"flags", PT_FLAGS32, PF_HEX, linkat_flags} } },
327327
/* PPME_SYSCALL_FCHMODAT_E */{"fchmodat", EC_FILE, EF_MODIFIES_STATE, 0},
328-
/* PPME_SYSCALL_FCHMODAT_X */{"fchmodat", EC_FILE, EF_MODIFIES_STATE, 4, {{"res", PT_ERRNO, PF_DEC}, {"dirfd", PT_FD, PF_DEC}, {"filename", PT_FSPATH, PF_NA}, {"mode", PT_UINT32, PF_OCT} } },
328+
/* PPME_SYSCALL_FCHMODAT_X */{"fchmodat", EC_FILE, EF_MODIFIES_STATE, 4, {{"res", PT_ERRNO, PF_DEC}, {"dirfd", PT_FD, PF_DEC}, {"filename", PT_FSPATH, PF_NA}, {"mode", PT_MODES, PF_OCT, chmod_modes} } },
329329
/* PPME_SYSCALL_CHMOD_E */{"chmod", EC_FILE, EF_MODIFIES_STATE, 0},
330-
/* PPME_SYSCALL_CHMOD_X */{"chmod", EC_FILE, EF_MODIFIES_STATE, 3, {{"res", PT_ERRNO, PF_DEC}, {"filename", PT_FSPATH, PF_NA}, {"mode", PT_UINT32, PF_OCT} } },
330+
/* PPME_SYSCALL_CHMOD_X */{"chmod", EC_FILE, EF_MODIFIES_STATE, 3, {{"res", PT_ERRNO, PF_DEC}, {"filename", PT_FSPATH, PF_NA}, {"mode", PT_MODES, PF_OCT, chmod_modes} } },
331331
/* PPME_SYSCALL_FCHMOD_E */{"fchmod", EC_FILE, EF_MODIFIES_STATE, 0},
332-
/* PPME_SYSCALL_FCHMOD_X */{"fchmod", EC_FILE, EF_MODIFIES_STATE, 3, {{"res", PT_ERRNO, PF_DEC}, {"fd", PT_FD, PF_DEC}, {"mode", PT_UINT32, PF_OCT} } }
332+
/* PPME_SYSCALL_FCHMOD_X */{"fchmod", EC_FILE, EF_MODIFIES_STATE, 3, {{"res", PT_ERRNO, PF_DEC}, {"fd", PT_FD, PF_DEC}, {"mode", PT_MODES, PF_OCT, chmod_modes} } }
333333

334334
/* NB: Starting from scap version 1.2, event types will no longer be changed when an event is modified, and the only kind of change permitted for pre-existent events is adding parameters.
335335
* New event types are allowed only for new syscalls or new internal events.

driver/modes_table.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
3+
Copyright (c) 2013-2019 Draios Inc. dba Sysdig.
4+
5+
This file is dual licensed under either the MIT or GPL 2. See MIT.txt
6+
or GPL2.txt for full copies of the license.
7+
8+
*/
9+
10+
#include "ppm_events_public.h"
11+
12+
const struct ppm_name_value chmod_modes[] = {
13+
{"S_IXOTH", PPM_S_IXOTH},
14+
{"S_IWOTH", PPM_S_IWOTH},
15+
{"S_IROTH", PPM_S_IROTH},
16+
{"S_IXGRP", PPM_S_IXGRP},
17+
{"S_IWGRP", PPM_S_IWGRP},
18+
{"S_IRGRP", PPM_S_IRGRP},
19+
{"S_IXUSR", PPM_S_IXUSR},
20+
{"S_IWUSR", PPM_S_IWUSR},
21+
{"S_IRUSR", PPM_S_IRUSR},
22+
{"S_ISVTX", PPM_S_ISVTX},
23+
{"S_ISGID", PPM_S_ISGID},
24+
{"S_ISUID", PPM_S_ISUID},
25+
{0, 0},
26+
};

driver/ppm_events.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,7 @@ int val_to_ring(struct event_filler_arguments *args, uint64_t val, u32 val_len,
657657
break;
658658
case PT_FLAGS32:
659659
case PT_UINT32:
660+
case PT_MODES:
660661
case PT_UID:
661662
case PT_GID:
662663
case PT_SIGSET:

driver/ppm_events_public.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,8 @@ enum ppm_param_type {
13641364
PT_IPV6NET = 39, /* An IPv6 network. */
13651365
PT_IPADDR = 40, /* Either an IPv4 or IPv6 address. The length indicates which one it is. */
13661366
PT_IPNET = 41, /* Either an IPv4 or IPv6 network. The length indicates which one it is. */
1367-
PT_MAX = 42 /* array size */
1367+
PT_MAX = 42, /* array size */
1368+
PT_MODES = 43 /* a 32 bit bitmask to represent file modes. */
13681369
};
13691370

13701371
enum ppm_print_format {
@@ -1494,6 +1495,7 @@ extern const struct ppm_name_value access_flags[];
14941495
extern const struct ppm_name_value pf_flags[];
14951496
extern const struct ppm_name_value unlinkat_flags[];
14961497
extern const struct ppm_name_value linkat_flags[];
1498+
extern const struct ppm_name_value chmod_modes[];
14971499

14981500
extern const struct ppm_param_info sockopt_dynamic_param[];
14991501
extern const struct ppm_param_info ptrace_dynamic_param[];

driver/ppm_fillers.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4842,7 +4842,7 @@ int f_sys_fchmodat_x(struct event_filler_arguments *args)
48424842
* mode
48434843
*/
48444844
syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val);
4845-
res = val_to_ring(args, val, 0, false, 0);
4845+
res = val_to_ring(args, chmod_modes_to_scap(val), 0, false, 0);
48464846
if (unlikely(res != PPM_SUCCESS))
48474847
return res;
48484848

@@ -4871,8 +4871,8 @@ int f_sys_chmod_x(struct event_filler_arguments *args)
48714871
/*
48724872
* mode
48734873
*/
4874-
syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val);
4875-
res = val_to_ring(args, val, 0, false, 0);
4874+
syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val);
4875+
res = val_to_ring(args, chmod_modes_to_scap(val), 0, false, 0);
48764876
if (unlikely(res != PPM_SUCCESS))
48774877
return res;
48784878

@@ -4902,8 +4902,8 @@ int f_sys_fchmod_x(struct event_filler_arguments *args)
49024902
/*
49034903
* mode
49044904
*/
4905-
syscall_get_arguments_deprecated(current, args->regs, 1, 1, &val);
4906-
res = val_to_ring(args, val, 0, false, 0);
4905+
syscall_get_arguments_deprecated(current, args->regs, 2, 1, &val);
4906+
res = val_to_ring(args, chmod_modes_to_scap(val), 0, false, 0);
49074907
if (unlikely(res != PPM_SUCCESS))
49084908
return res;
49094909

driver/ppm_flag_helpers.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,4 +1268,58 @@ static __always_inline u32 linkat_flags_to_scap(unsigned long flags)
12681268
return res;
12691269
}
12701270

1271+
static __always_inline u32 chmod_modes_to_scap(unsigned long modes)
1272+
{
1273+
u32 res = 0;
1274+
if (modes & S_IRUSR)
1275+
res |= PPM_S_IRUSR;
1276+
1277+
if (modes & S_IWUSR)
1278+
res |= PPM_S_IWUSR;
1279+
1280+
if (modes & S_IXUSR)
1281+
res |= PPM_S_IXUSR;
1282+
1283+
/*
1284+
* PPM_S_IRWXU == S_IRUSR | S_IWUSR | S_IXUSR
1285+
*/
1286+
1287+
if (modes & S_IRGRP)
1288+
res |= PPM_S_IRGRP;
1289+
1290+
if (modes & S_IWGRP)
1291+
res |= PPM_S_IWGRP;
1292+
1293+
if (modes & S_IXGRP)
1294+
res |= PPM_S_IXGRP;
1295+
1296+
/*
1297+
* PPM_S_IRWXG == S_IRGRP | S_IWGRP | S_IXGRP
1298+
*/
1299+
1300+
if (modes & S_IROTH)
1301+
res |= PPM_S_IROTH;
1302+
1303+
if (modes & S_IWOTH)
1304+
res |= PPM_S_IWOTH;
1305+
1306+
if (modes & S_IXOTH)
1307+
res |= PPM_S_IXOTH;
1308+
1309+
/*
1310+
* PPM_S_IRWXO == S_IROTH | S_IWOTH | S_IXOTH
1311+
*/
1312+
1313+
if (modes & S_ISUID)
1314+
res |= PPM_S_ISUID;
1315+
1316+
if (modes & S_ISGID)
1317+
res |= PPM_S_ISGID;
1318+
1319+
if (modes & S_ISVTX)
1320+
res |= PPM_S_ISVTX;
1321+
1322+
return res;
1323+
}
1324+
12711325
#endif /* PPM_FLAG_HELPERS_H_ */

userspace/libscap/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ list(APPEND targetfiles
3232
syscall_info_table.c
3333
../../driver/dynamic_params_table.c
3434
../../driver/event_table.c
35-
../../driver/flags_table.c)
35+
../../driver/flags_table.c
36+
../../driver/modes_table.c)
3637

3738
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
3839
list(APPEND targetfiles

0 commit comments

Comments
 (0)