2 * Check decoding of chown/chown32/lchown/lchown32/fchown/fchown32 syscalls.
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2016-2018 The strace developers.
8 * SPDX-License-Identifier: GPL-2.0-or-later
15 #ifdef UGID_TYPE_IS_SHORT
16 # define UGID_TYPE short
17 # define GETEUID syscall(__NR_geteuid)
18 # define GETEGID syscall(__NR_getegid)
19 # define CHECK_OVERFLOWUID(arg) check_overflowuid(arg)
20 # define CHECK_OVERFLOWGID(arg) check_overflowgid(arg)
22 # define UGID_TYPE int
23 # define GETEUID geteuid()
24 # define GETEGID getegid()
25 # define CHECK_OVERFLOWUID(arg)
26 # define CHECK_OVERFLOWGID(arg)
29 #define UNLINK_SAMPLE \
32 perror_msg_and_fail("unlink"); \
35 #define CLOSE_SAMPLE \
38 perror_msg_and_fail("close"); \
41 #ifdef ACCESS_BY_DESCRIPTOR
42 # define SYSCALL_ARG1 fd
43 # define FMT_ARG1 "%d"
44 # define EOK_CMD CLOSE_SAMPLE
45 # define CLEANUP_CMD UNLINK_SAMPLE
47 # define SYSCALL_ARG1 sample
48 # define FMT_ARG1 "\"%s\""
49 # define EOK_CMD UNLINK_SAMPLE
50 # define CLEANUP_CMD CLOSE_SAMPLE
54 ugid2int(const unsigned UGID_TYPE id)
56 if ((unsigned UGID_TYPE) -1U == id)
63 print_int(const unsigned int num)
72 num_matches_id(const unsigned int num, const unsigned int id)
74 return num == id || num == -1U;
77 #define PAIR(val) { val, gid }, { uid, val }
82 static const char sample[] = SYSCALL_NAME "_sample";
84 unsigned int uid = GETEUID;
85 CHECK_OVERFLOWUID(uid);
86 unsigned int gid = GETEGID;
87 CHECK_OVERFLOWUID(gid);
93 { (unsigned long) 0xffffffff00000000ULL | uid, gid },
94 { uid, (unsigned long) 0xffffffff00000000ULL | gid },
97 { 0xffff0000U | uid, gid },
98 { uid, 0xffff0000U | gid },
102 PAIR((long) 0xfacefeeddeadbeefULL)
105 int fd = open(sample, O_RDONLY | O_CREAT, 0400);
107 perror_msg_and_fail("open");
114 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
115 const unsigned int unum = ugid2int(tests[i].uid);
116 const unsigned int gnum = ugid2int(tests[i].gid);
118 if (num_matches_id(unum, uid) &&
119 num_matches_id(gnum, gid)) {
129 const long rc = syscall(SYSCALL_NR, SYSCALL_ARG1,
130 tests[i].uid, tests[i].gid);
131 const char *errstr = sprintrc(rc);
132 printf("%s(" FMT_ARG1, SYSCALL_NAME, SYSCALL_ARG1);
135 printf(") = %s\n", errstr);
138 puts("+++ exited with 0 +++");