1 /*
2 * Check decoding of fchmod syscall.
3 *
4 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
5 * Copyright (c) 2016 Dmitry V. Levin <ldv@strace.io>
6 * Copyright (c) 2016-2022 The strace developers.
7 * All rights reserved.
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12 #include "tests.h"
13 #include "scno.h"
14
15 #ifdef __NR_fchmod
16
17 # include <fcntl.h>
18 # include <stdio.h>
19 # include <unistd.h>
20
21 # include "secontext.h"
22
23 int
24 main(void)
25 {
26 /*
27 * Make sure the current workdir of the tracee
28 * is different from the current workdir of the tracer.
29 */
30 create_and_enter_subdir("fchmod_subdir");
31
32 char *my_secontext = SECONTEXT_PID_MY();
33
34 static const char sample[] = "fchmod_sample_file";
35 (void) unlink(sample);
36 int fd = open(sample, O_CREAT|O_RDONLY, 0400);
37 if (fd == -1)
38 perror_msg_and_fail("open(\"%s\")", sample);
39
40 static const char sample_del[] = "fchmod_sample_file (deleted)";
41 (void) unlink(sample_del);
42 int fd_del = open(sample_del, O_CREAT|O_RDONLY, 0400);
43 if (fd_del == -1)
44 perror_msg_and_fail("open(\"%s\")", sample);
45
46 # ifdef YFLAG
47 char *sample_realpath = get_fd_path(fd);
48 char *sample_del_realpath = get_fd_path(fd_del);
49 # endif
50
51 const char *sample_secontext = SECONTEXT_FILE(sample);
52 long rc = syscall(__NR_fchmod, fd, 0600);
53 # ifdef YFLAG
54 printf("%s%s(%d<%s>%s, 0600) = %s\n",
55 # else
56 printf("%s%s(%d%s, 0600) = %s\n",
57 # endif
58 my_secontext, "fchmod",
59 fd,
60 # ifdef YFLAG
61 sample_realpath,
62 # endif
63 sample_secontext,
64 sprintrc(rc));
65
66 const char *sample_del_secontext = SECONTEXT_FILE(sample_del);
67 rc = syscall(__NR_fchmod, fd_del, 0600);
68 # ifdef YFLAG
69 printf("%s%s(%d<%s>%s, 0600) = %s\n",
70 # else
71 printf("%s%s(%d%s, 0600) = %s\n",
72 # endif
73 my_secontext, "fchmod",
74 fd_del,
75 # ifdef YFLAG
76 sample_del_realpath,
77 # endif
78 sample_del_secontext,
79 sprintrc(rc));
80
81 if (unlink(sample))
82 perror_msg_and_fail("unlink(\"%s\")", sample);
83
84 rc = syscall(__NR_fchmod, fd, 051);
85 # ifdef YFLAG
86 printf("%s%s(%d<%s>(deleted)%s, 051) = %s\n",
87 # else
88 printf("%s%s(%d%s, 051) = %s\n",
89 # endif
90 my_secontext, "fchmod",
91 fd,
92 # ifdef YFLAG
93 sample_realpath,
94 # endif
95 sample_secontext,
96 sprintrc(rc));
97
98 if (unlink(sample_del))
99 perror_msg_and_fail("unlink(\"%s\")", sample_del);
100
101 rc = syscall(__NR_fchmod, fd_del, 051);
102 # ifdef YFLAG
103 printf("%s%s(%d<%s>(deleted)%s, 051) = %s\n",
104 # else
105 printf("%s%s(%d%s, 051) = %s\n",
106 # endif
107 my_secontext, "fchmod",
108 fd_del,
109 # ifdef YFLAG
110 sample_del_realpath,
111 # endif
112 sample_del_secontext,
113 sprintrc(rc));
114
115 rc = syscall(__NR_fchmod, fd, 004);
116 # ifdef YFLAG
117 printf("%s%s(%d<%s>(deleted)%s, 004) = %s\n",
118 # else
119 printf("%s%s(%d%s, 004) = %s\n",
120 # endif
121 my_secontext, "fchmod",
122 fd,
123 # ifdef YFLAG
124 sample_realpath,
125 # endif
126 sample_secontext,
127 sprintrc(rc));
128
129 leave_and_remove_subdir();
130
131 puts("+++ exited with 0 +++");
132 return 0;
133 }
134
135 #else
136
137 SKIP_MAIN_UNDEFINED("__NR_fchmod")
138
139 #endif