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-2017 The strace developers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifdef UGID_TYPE_IS_SHORT
36 # define UGID_TYPE short
37 # define GETEUID syscall(__NR_geteuid)
38 # define GETEGID syscall(__NR_getegid)
39 # define CHECK_OVERFLOWUID(arg) check_overflowuid(arg)
40 # define CHECK_OVERFLOWGID(arg) check_overflowgid(arg)
42 # define UGID_TYPE int
43 # define GETEUID geteuid()
44 # define GETEGID getegid()
45 # define CHECK_OVERFLOWUID(arg)
46 # define CHECK_OVERFLOWGID(arg)
49 #define UNLINK_SAMPLE \
52 perror_msg_and_fail("unlink"); \
55 #define CLOSE_SAMPLE \
58 perror_msg_and_fail("close"); \
61 #ifdef ACCESS_BY_DESCRIPTOR
62 # define SYSCALL_ARG1 fd
63 # define FMT_ARG1 "%d"
64 # define EOK_CMD CLOSE_SAMPLE
65 # define CLEANUP_CMD UNLINK_SAMPLE
67 # define SYSCALL_ARG1 sample
68 # define FMT_ARG1 "\"%s\""
69 # define EOK_CMD UNLINK_SAMPLE
70 # define CLEANUP_CMD CLOSE_SAMPLE
74 ugid2int(const unsigned UGID_TYPE id)
76 if ((unsigned UGID_TYPE) -1U == id)
83 print_int(const unsigned int num)
92 num_matches_id(const unsigned int num, const unsigned int id)
94 return num == id || num == -1U;
97 #define PAIR(val) { val, gid }, { uid, val }
102 static const char sample[] = SYSCALL_NAME "_sample";
104 unsigned int uid = GETEUID;
105 CHECK_OVERFLOWUID(uid);
106 unsigned int gid = GETEGID;
107 CHECK_OVERFLOWUID(gid);
113 { (unsigned long) 0xffffffff00000000ULL | uid, gid },
114 { uid, (unsigned long) 0xffffffff00000000ULL | gid },
117 { 0xffff0000U | uid, gid },
118 { uid, 0xffff0000U | gid },
122 PAIR((long) 0xfacefeeddeadbeefULL)
125 int fd = open(sample, O_RDONLY | O_CREAT, 0400);
127 perror_msg_and_fail("open");
134 for (i = 0; i < ARRAY_SIZE(tests); ++i) {
135 const unsigned int unum = ugid2int(tests[i].uid);
136 const unsigned int gnum = ugid2int(tests[i].gid);
138 if (num_matches_id(unum, uid) &&
139 num_matches_id(gnum, gid)) {
149 const long rc = syscall(SYSCALL_NR, SYSCALL_ARG1,
150 tests[i].uid, tests[i].gid);
151 const char *errstr = sprintrc(rc);
152 printf("%s(" FMT_ARG1, SYSCALL_NAME, SYSCALL_ARG1);
155 printf(") = %s\n", errstr);
158 puts("+++ exited with 0 +++");