1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
5 */
6
7 /*\
8 * [Description]
9 *
10 * Tests basic error handling of the quotactl syscall without visible quota files
11 * (use quotactl and quotactl_fd syscall):
12 *
13 * - EFAULT when addr or special is invalid
14 * - EINVAL when cmd or type is invalid
15 * - ENOTBLK when special is not a block device
16 * - ERANGE when cmd is Q_SETQUOTA, but the specified limits are out of the range
17 * allowed by the quota format
18 * - EPERM when the caller lacked the required privilege (CAP_SYS_ADMIN) for the
19 * specified operation
20 * - ENOSYS when cmd is Q_QUOTAON, but the fd refers to a socket
21 *
22 * Minimum e2fsprogs version required is 1.43.
23 */
24
25 #include <errno.h>
26 #include <sys/quota.h>
27 #include <sys/socket.h>
28 #include "tst_test.h"
29 #include "tst_capability.h"
30 #include "quotactl_syscall_var.h"
31
32 #define OPTION_INVALID 999
33
34 static int32_t fmt_id = QFMT_VFS_V1;
35 static int test_id;
36 static int getnextquota_nsup, socket_fd = -1;
37
38 static struct if_nextdqblk res_ndq;
39
40 static struct dqblk set_dqmax = {
41 .dqb_bsoftlimit = 0x7fffffffffffffffLL, /* 2^63-1 */
42 .dqb_valid = QIF_BLIMITS
43 };
44
45 static struct tst_cap dropadmin = {
46 .action = TST_CAP_DROP,
47 .id = CAP_SYS_ADMIN,
48 .name = "CAP_SYS_ADMIN",
49 };
50
51 static struct tst_cap needadmin = {
52 .action = TST_CAP_REQ,
53 .id = CAP_SYS_ADMIN,
54 .name = "CAP_SYS_ADMIN",
55 };
56
57 static struct tcase {
58 int cmd;
59 int *id;
60 void *addr;
61 int exp_err;
62 int on_flag;
63 char *des;
64 } tcases[] = {
65 {QCMD(Q_SETQUOTA, USRQUOTA), &fmt_id, NULL, EFAULT, 1,
66 "EFAULT when addr or special is invalid"},
67
68 {QCMD(OPTION_INVALID, USRQUOTA), &fmt_id, NULL, EINVAL, 0,
69 "EINVAL when cmd or type is invalid"},
70
71 {QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, NULL, ENOTBLK, 0,
72 "ENOTBLK when special is not a block device"},
73
74 {QCMD(Q_SETQUOTA, USRQUOTA), &test_id, &set_dqmax, ERANGE, 1,
75 "ERANGE when cmd is Q_SETQUOTA, but the specified limits are out of the range"},
76
77 {QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, NULL, EPERM, 0,
78 "EPERM when the caller lacks required privilege(CAP_SYS_ADMIN)"},
79
80 {QCMD(Q_QUOTAON, USRQUOTA), &fmt_id, NULL, ENOSYS, 0,
81 "EINVAL when cmd is Q_QUOTAON, but the fd refers to a socket"}
82 };
83
verify_quotactl(unsigned int n)84 static void verify_quotactl(unsigned int n)
85 {
86 struct tcase *tc = &tcases[n];
87 int quota_on = 0;
88 int drop_flag = 0;
89
90 tst_res(TINFO, "Testing %s", tc->des);
91 if (tc->cmd == QCMD(Q_GETNEXTQUOTA, USRQUOTA) && getnextquota_nsup) {
92 tst_res(TCONF, "current system doesn't support Q_GETNEXTQUOTA");
93 return;
94 }
95
96 if (tc->on_flag) {
97 TST_EXP_PASS_SILENT(do_quotactl(fd, QCMD(Q_QUOTAON, USRQUOTA), tst_device->dev,
98 fmt_id, NULL), "do_quotactl(QCMD(Q_QUOTAON, USRQUOTA))");
99 if (!TST_PASS)
100 return;
101 quota_on = 1;
102 }
103
104 if (tc->exp_err == EPERM) {
105 tst_cap_action(&dropadmin);
106 drop_flag = 1;
107 }
108
109 if (tst_variant) {
110 if (tc->exp_err == ENOTBLK) {
111 tst_res(TCONF, "quotactl_fd() doesn't have this error, skip");
112 return;
113 }
114 if (tc->exp_err == ENOSYS) {
115 TST_EXP_FAIL(syscall(__NR_quotactl_fd, socket_fd, tc->cmd, *tc->id,
116 tc->addr), tc->exp_err, "syscall(quotactl_fd)");
117 return;
118 }
119 } else {
120 if (tc->exp_err == ENOSYS) {
121 tst_res(TCONF, "quotactl() doesn't use fd, skip");
122 return;
123 }
124 }
125 if (tc->exp_err == ENOTBLK)
126 TST_EXP_FAIL(do_quotactl(fd, tc->cmd, "/dev/null", *tc->id, tc->addr),
127 ENOTBLK, "do_quotactl()");
128 else
129 TST_EXP_FAIL(do_quotactl(fd, tc->cmd, tst_device->dev, *tc->id, tc->addr),
130 tc->exp_err, "do_quotactl()");
131
132 if (quota_on) {
133 TST_EXP_PASS_SILENT(do_quotactl(fd, QCMD(Q_QUOTAOFF, USRQUOTA), tst_device->dev,
134 fmt_id, NULL), "do_quotactl(QCMD(Q_QUOTAOFF, USRQUOTA)");
135 if (!TST_PASS)
136 return;
137 }
138
139 if (drop_flag)
140 tst_cap_action(&needadmin);
141 }
142
setup(void)143 static void setup(void)
144 {
145 unsigned int i;
146
147 quotactl_info();
148
149 socket_fd = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
150 fd = SAFE_OPEN(MNTPOINT, O_RDONLY);
151 TEST(do_quotactl(fd, QCMD(Q_GETNEXTQUOTA, USRQUOTA), tst_device->dev,
152 test_id, (void *) &res_ndq));
153 if (TST_ERR == EINVAL || TST_ERR == ENOSYS)
154 getnextquota_nsup = 1;
155
156 for (i = 0; i < ARRAY_SIZE(tcases); i++) {
157 if (!tcases[i].addr)
158 tcases[i].addr = tst_get_bad_addr(NULL);
159 }
160 }
161
cleanup(void)162 static void cleanup(void)
163 {
164 if (fd > -1)
165 SAFE_CLOSE(fd);
166 if (socket_fd > -1)
167 SAFE_CLOSE(socket_fd);
168 }
169
170 static struct tst_test test = {
171 .setup = setup,
172 .cleanup = cleanup,
173 .needs_kconfigs = (const char *[]) {
174 "CONFIG_QFMT_V2",
175 NULL
176 },
177 .tcnt = ARRAY_SIZE(tcases),
178 .test = verify_quotactl,
179 .dev_fs_opts = (const char *const[]){"-O quota", NULL},
180 .dev_fs_type = "ext4",
181 .mntpoint = MNTPOINT,
182 .mount_device = 1,
183 .needs_root = 1,
184 .test_variants = QUOTACTL_SYSCALL_VARIANTS,
185 .needs_cmds = (const char *[]) {
186 "mkfs.ext4 >= 1.43.0",
187 NULL
188 }
189 };
190