xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/openat/openat04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Check setgid strip logic whether works correctly when creating tmpfile under
11  * filesystem without POSIX ACL supported(by using noacl mount option). Test it
12  * with umask S_IXGRP and also check file mode whether has filtered S_IXGRP.
13  *
14  * Fixed in:
15  *
16  *  commit ac6800e279a22b28f4fc21439843025a0d5bf03e
17  *  Author: Yang Xu <[email protected]>
18  *  Date:   Thu July 14 14:11:26 2022 +0800
19  *
20  *  fs: Add missing umask strip in vfs_tmpfile
21  *
22  * The most code is pasted form creat09.c.
23  */
24 
25 #define _GNU_SOURCE
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <pwd.h>
29 #include <sys/mount.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include "tst_test.h"
33 #include "lapi/fcntl.h"
34 #include "tst_uid.h"
35 #include "tst_safe_file_at.h"
36 
37 #define MODE_RWX        0777
38 #define MODE_SGID       (S_ISGID|0777)
39 #define MNTPOINT	"mntpoint"
40 #define WORKDIR		MNTPOINT "/testdir"
41 #define OPEN_FILE	"open.tmp"
42 
43 static gid_t free_gid;
44 static int tmpfile_fd = -1, dir_fd = -1, mount_flag;
45 static struct passwd *ltpuser;
46 
do_mount(const char * source,const char * target,const char * filesystemtype,unsigned long mountflags,const void * data)47 static void do_mount(const char *source, const char *target,
48 	const char *filesystemtype, unsigned long mountflags,
49 	const void *data)
50 {
51 	TEST(mount(source, target, filesystemtype, mountflags, data));
52 
53 	if (TST_RET == -1 && TST_ERR == EINVAL)
54 		tst_brk(TCONF, "Kernel does not support noacl feature");
55 
56 	if (TST_RET == -1) {
57 		tst_brk(TBROK | TTERRNO, "mount(%s, %s, %s, %lu, %p) failed",
58 			source, target, filesystemtype, mountflags, data);
59 	}
60 
61 	if (TST_RET)
62 		tst_brk(TBROK, "Invalid mount return value %ld", TST_RET);
63 
64 	mount_flag = 1;
65 }
66 
open_tmpfile_supported(int dirfd)67 static void open_tmpfile_supported(int dirfd)
68 {
69 	TEST(openat(dirfd, ".", O_TMPFILE | O_RDWR, S_IXGRP | S_ISGID));
70 
71 	if (TST_RET == -1) {
72 		if (errno == ENOTSUP)
73 			tst_brk(TCONF, "fs doesn't support O_TMPFILE");
74 		else
75 			tst_brk(TBROK | TTERRNO, "openat(%d, O_TMPFILE) failed", dirfd);
76 	}
77 
78 	if (TST_RET < 0)
79 		tst_brk(TBROK, "Invalid openat return value %ld", TST_RET);
80 
81 	SAFE_CLOSE(TST_RET);
82 }
83 
setup(void)84 static void setup(void)
85 {
86 	struct stat buf;
87 
88 	ltpuser = SAFE_GETPWNAM("nobody");
89 
90 	do_mount(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, "noacl");
91 
92 	tst_res(TINFO, "User nobody: uid = %d, gid = %d", (int)ltpuser->pw_uid,
93 		(int)ltpuser->pw_gid);
94 	free_gid = tst_get_free_gid(ltpuser->pw_gid);
95 
96 	/* Create directories and set permissions */
97 	SAFE_MKDIR(WORKDIR, MODE_RWX);
98 	dir_fd = SAFE_OPEN(WORKDIR, O_RDONLY, O_DIRECTORY);
99 	open_tmpfile_supported(dir_fd);
100 
101 	SAFE_CHOWN(WORKDIR, ltpuser->pw_uid, free_gid);
102 	SAFE_CHMOD(WORKDIR, MODE_SGID);
103 	SAFE_STAT(WORKDIR, &buf);
104 
105 	if (!(buf.st_mode & S_ISGID))
106 		tst_brk(TBROK, "%s: Setgid bit not set", WORKDIR);
107 
108 	if (buf.st_gid != free_gid) {
109 		tst_brk(TBROK, "%s: Incorrect group, %u != %u", WORKDIR,
110 			buf.st_gid, free_gid);
111 	}
112 
113 	/* Switch user */
114 	SAFE_SETGID(ltpuser->pw_gid);
115 	SAFE_SETREUID(-1, ltpuser->pw_uid);
116 }
117 
file_test(int dfd,const char * path,int flags)118 static void file_test(int dfd, const char *path, int flags)
119 {
120 	struct stat buf;
121 
122 	SAFE_FSTATAT(dfd, path, &buf, flags);
123 
124 	TST_EXP_EQ_LI(buf.st_gid, free_gid);
125 
126 	if (buf.st_mode & S_ISGID)
127 		tst_res(TFAIL, "%s: Setgid bit is set", path);
128 	else
129 		tst_res(TPASS, "%s: Setgid bit not set", path);
130 
131 	if (buf.st_mode & S_IXGRP)
132 		tst_res(TFAIL, "%s: S_IXGRP bit is set", path);
133 	else
134 		tst_res(TPASS, "%s: S_IXGRP bit is not set", path);
135 }
136 
run(void)137 static void run(void)
138 {
139 	char path[PATH_MAX];
140 
141 	umask(S_IXGRP);
142 	tmpfile_fd = SAFE_OPENAT(dir_fd, ".", O_TMPFILE | O_RDWR, MODE_SGID);
143 	snprintf(path, PATH_MAX, "/proc/self/fd/%d", tmpfile_fd);
144 	SAFE_LINKAT(AT_FDCWD, path, dir_fd, OPEN_FILE, AT_SYMLINK_FOLLOW);
145 	file_test(dir_fd, OPEN_FILE, 0);
146 	SAFE_CLOSE(tmpfile_fd);
147 	/* Cleanup between loops */
148 	tst_purge_dir(WORKDIR);
149 }
150 
cleanup(void)151 static void cleanup(void)
152 {
153 	SAFE_SETREUID(-1, 0);
154 
155 	if (tmpfile_fd >= 0)
156 		SAFE_CLOSE(tmpfile_fd);
157 	if (dir_fd >= 0)
158 		SAFE_CLOSE(dir_fd);
159 	if (mount_flag && tst_umount(MNTPOINT))
160 		tst_res(TWARN | TERRNO, "umount(%s)", MNTPOINT);
161 }
162 
163 static struct tst_test test = {
164 	.test_all = run,
165 	.setup = setup,
166 	.cleanup = cleanup,
167 	.needs_root = 1,
168 	.all_filesystems = 1,
169 	.format_device = 1,
170 	.mntpoint = MNTPOINT,
171 	.skip_filesystems = (const char*[]) {
172 		"exfat",
173 		"ntfs",
174 		"vfat",
175 		NULL
176 	},
177 	.tags = (const struct tst_tag[]) {
178 		{"linux-git", "ac6800e279a2"},
179 		{"linux-git", "426b4ca2d6a5"},
180 		{}
181 	},
182 };
183