File: fchmodat.c

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (108 lines) | stat: -rw-r--r-- 2,602 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Check decoding of fchmodat syscall.
 *
 * Copyright (c) 2016-2018 Dmitry V. Levin <ldv@strace.io>
 * Copyright (c) 2016-2021 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"
#include "scno.h"

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

#include "secontext.h"

int
main(void)
{
	/*
	 * Make sure the current workdir of the tracee
	 * is different from the current workdir of the tracer.
	 */
	create_and_enter_subdir("fchmodat_subdir");

	char *my_secontext = SECONTEXT_PID_MY();

	static const char sample[] = "fchmodat_sample_file";
	if (open(sample, O_RDONLY | O_CREAT, 0400) < 0)
		perror_msg_and_fail("open");

	char *sample_secontext = SECONTEXT_FILE(sample);

	/*
	 * Tests with AT_FDCWD.
	 */

	long rc = syscall(__NR_fchmodat, -100, sample, 0600);
	printf("%s%s(AT_FDCWD, \"%s\"%s, 0600) = %s\n",
	       my_secontext, "fchmodat",
	       sample, sample_secontext,
	       sprintrc(rc));

	if (unlink(sample))
		perror_msg_and_fail("unlink");

	rc = syscall(__NR_fchmodat, -100, sample, 051);
	printf("%s%s(AT_FDCWD, \"%s\", 051) = %s\n",
	       my_secontext, "fchmodat",
	       sample, sprintrc(rc));

	rc = syscall(__NR_fchmodat, -100, sample, 004);
	printf("%s%s(AT_FDCWD, \"%s\", 004) = %s\n",
	       my_secontext, "fchmodat",
	       sample, sprintrc(rc));

	/*
	 * Tests with dirfd.
	 */

	int cwd_fd = get_dir_fd(".");
	char *cwd = get_fd_path(cwd_fd);
	char *cwd_secontext = SECONTEXT_FILE(".");
	char *sample_realpath = xasprintf("%s/%s", cwd, sample);

	/* no file */
	rc = syscall(__NR_fchmodat, cwd_fd, sample, 0400);
	printf("%s%s(%d%s, \"%s\", 0400) = %s\n",
	       my_secontext, "fchmodat",
	       cwd_fd, cwd_secontext,
	       sample,
	       sprintrc(rc));

	if (open(sample, O_RDONLY | O_CREAT, 0400) < 0)
		perror_msg_and_fail("open");

	rc = syscall(__NR_fchmodat, cwd_fd, sample, 0400);
	printf("%s%s(%d%s, \"%s\"%s, 0400) = %s\n",
	       my_secontext, "fchmodat",
	       cwd_fd, cwd_secontext,
	       sample, sample_secontext,
	       sprintrc(rc));

	/* cwd_fd ignored when path is absolute */
	if (chdir("../.."))
		perror_msg_and_fail("chdir");

	rc = syscall(__NR_fchmodat, cwd_fd, sample_realpath, 0400);
	printf("%s%s(%d%s, \"%s\"%s, 0400) = %s\n",
	       my_secontext, "fchmodat",
	       cwd_fd, cwd_secontext,
	       sample_realpath, sample_secontext,
	       sprintrc(rc));

	if (fchdir(cwd_fd))
		perror_msg_and_fail("fchdir");

	if (unlink(sample))
		perror_msg_and_fail("unlink");

	leave_and_remove_subdir();

	puts("+++ exited with 0 +++");
	return 0;
}