1 /*
2 * Check decoding of quotactl syscall.
3 *
4 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
5 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
6 * Copyright (c) 2016-2018 The strace developers.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "tests.h"
33
34 #include <asm/unistd.h>
35
36 #if defined(__NR_quotactl) && \
37 (defined(HAVE_LINUX_QUOTA_H) || defined(HAVE_SYS_QUOTA_H))
38
39 # include <inttypes.h>
40 # include <stdint.h>
41 # include <stdio.h>
42 # include <string.h>
43 # include <unistd.h>
44
45 # include "quotactl.h"
46
47 # ifndef HAVE_LINUX_QUOTA_H
48 /* Some dirty hacks in order to make sys/quota.h usable as a backup */
49
50 # define if_dqblk dqblk
51 # define if_nextdqblk nextdqblk
52 # define if_dqinfo dqinfo
53
54 # endif /* !HAVE_LINUX_QUOTA_H */
55
56 # ifndef Q_GETNEXTQUOTA
57
58 # define Q_GETNEXTQUOTA 0x800009
59
60 struct if_nextdqblk {
61 uint64_t dqb_bhardlimit;
62 uint64_t dqb_bsoftlimit;
63 uint64_t dqb_curspace;
64 uint64_t dqb_ihardlimit;
65 uint64_t dqb_isoftlimit;
66 uint64_t dqb_curinodes;
67 uint64_t dqb_btime;
68 uint64_t dqb_itime;
69 uint32_t dqb_valid;
70 uint32_t dqb_id;
71 };
72 # endif /* !Q_GETNEXTQUOTA */
73
74 # include "xlat.h"
75 # include "xlat/quota_formats.h"
76 # include "xlat/if_dqblk_valid.h"
77 # include "xlat/if_dqinfo_flags.h"
78 # include "xlat/if_dqinfo_valid.h"
79
80 void
print_dqblk(long rc,void * ptr,void * arg)81 print_dqblk(long rc, void *ptr, void *arg)
82 {
83 struct if_dqblk *db = ptr;
84 long out_arg = (long) arg;
85
86 if (((rc < 0) && out_arg) || (out_arg > 1)) {
87 printf("%p", db);
88 return;
89 }
90
91 PRINT_FIELD_U("{", *db, dqb_bhardlimit);
92 PRINT_FIELD_U(", ", *db, dqb_bsoftlimit);
93 PRINT_FIELD_U(", ", *db, dqb_curspace);
94 PRINT_FIELD_U(", ", *db, dqb_ihardlimit);
95 PRINT_FIELD_U(", ", *db, dqb_isoftlimit);
96 PRINT_FIELD_U(", ", *db, dqb_curinodes);
97
98 # if VERBOSE
99 PRINT_FIELD_U(", ", *db, dqb_btime);
100 PRINT_FIELD_U(", ", *db, dqb_itime);
101
102 printf(", dqb_valid=");
103 printflags(if_dqblk_valid, db->dqb_valid, "QIF_???");
104 # else
105 printf(", ...");
106 # endif /* !VERBOSE */
107 printf("}");
108 }
109
110 void
print_nextdqblk(long rc,void * ptr,void * arg)111 print_nextdqblk(long rc, void *ptr, void *arg)
112 {
113 struct if_nextdqblk *db = ptr;
114 long out_arg = (long) arg;
115
116 if (((rc < 0) && out_arg) || (out_arg > 1)) {
117 printf("%p", db);
118 return;
119 }
120
121 PRINT_FIELD_U("{", *db, dqb_bhardlimit);
122 PRINT_FIELD_U(", ", *db, dqb_bsoftlimit);
123 PRINT_FIELD_U(", ", *db, dqb_curspace);
124 PRINT_FIELD_U(", ", *db, dqb_ihardlimit);
125 PRINT_FIELD_U(", ", *db, dqb_isoftlimit);
126 PRINT_FIELD_U(", ", *db, dqb_curinodes);
127
128 # if VERBOSE
129 PRINT_FIELD_U(", ", *db, dqb_btime);
130 PRINT_FIELD_U(", ", *db, dqb_itime);
131
132 printf(", dqb_valid=");
133 printflags(if_dqblk_valid, db->dqb_valid, "QIF_???");
134
135 PRINT_FIELD_U(", ", *db, dqb_id);
136 # else
137 PRINT_FIELD_U(", ", *db, dqb_id);
138 printf(", ...");
139 # endif /* !VERBOSE */
140 printf("}");
141 }
142
143 void
print_dqinfo(long rc,void * ptr,void * arg)144 print_dqinfo(long rc, void *ptr, void *arg)
145 {
146 struct if_dqinfo *di = ptr;
147 long out_arg = (long) arg;
148
149 if (((rc < 0) && out_arg) || (out_arg > 1)) {
150 printf("%p", di);
151 return;
152 }
153
154 PRINT_FIELD_U("{", *di, dqi_bgrace);
155 PRINT_FIELD_U(", ", *di, dqi_igrace);
156
157 printf(", dqi_flags=");
158 printflags(if_dqinfo_flags, di->dqi_flags, "DQF_???");
159 printf(", dqi_valid=");
160 printflags(if_dqinfo_valid, di->dqi_valid, "IIF_???");
161 printf("}");
162 }
163
164 void
print_dqfmt(long rc,void * ptr,void * arg)165 print_dqfmt(long rc, void *ptr, void *arg)
166 {
167 uint32_t *fmtval = ptr;
168 long out_arg = (long) arg;
169 const char *fmtstr;
170
171 if (((rc < 0) && out_arg) || (out_arg > 1)) {
172 printf("%p", fmtval);
173 return;
174 }
175 printf("[");
176 switch (*fmtval) {
177 case 1:
178 fmtstr = "QFMT_VFS_OLD";
179 break;
180 case 2:
181 fmtstr = "QFMT_VFS_V0";
182 break;
183 case 3:
184 fmtstr = "QFMT_OCFS2";
185 break;
186 case 4:
187 fmtstr = "QFMT_VFS_V1";
188 break;
189 default:
190 printf("%#x /* QFMT_VFS_??? */]", *fmtval);
191 return;
192 }
193 printf("%s]", fmtstr);
194 }
195
196
197 int
main(void)198 main(void)
199 {
200 char *bogus_special = (char *) tail_alloc(1) + 1;
201 void *bogus_addr = (char *) tail_alloc(1) + 1;
202
203 char bogus_special_str[sizeof(void *) * 2 + sizeof("0x")];
204 char unterminated_str[sizeof(void *) * 2 + sizeof("0x")];
205
206 static char invalid_cmd_str[1024];
207 static char invalid_id_str[1024];
208 char *unterminated = tail_memdup(unterminated_data,
209 sizeof(unterminated_data));
210 TAIL_ALLOC_OBJECT_CONST_PTR(struct if_dqblk, dqblk);
211 TAIL_ALLOC_OBJECT_CONST_PTR(struct if_dqinfo, dqinfo);
212 TAIL_ALLOC_OBJECT_CONST_PTR(uint32_t, fmt);
213 TAIL_ALLOC_OBJECT_CONST_PTR(struct if_nextdqblk, nextdqblk);
214
215 snprintf(bogus_special_str, sizeof(bogus_special_str), "%p",
216 bogus_special);
217 snprintf(unterminated_str, sizeof(unterminated_str), "%p",
218 unterminated);
219
220
221 /* Invalid commands */
222
223 snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
224 "QCMD(%#x /* Q_??? */, %#x /* ???QUOTA */)",
225 QCMD_CMD(bogus_cmd), QCMD_TYPE(bogus_cmd));
226 check_quota(CQF_NONE, bogus_cmd, invalid_cmd_str,
227 bogus_special, bogus_special_str, bogus_id, bogus_addr);
228
229 snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
230 "QCMD(0 /* Q_??? */, USRQUOTA)");
231 check_quota(CQF_ADDR_STR, 0, invalid_cmd_str,
232 ARG_STR(NULL), -1, ARG_STR(NULL));
233
234
235 /* Q_QUOTAON */
236
237 check_quota(CQF_ID_STR | CQF_ADDR_STR,
238 ARG_STR(QCMD(Q_QUOTAON, USRQUOTA)),
239 ARG_STR("/dev/bogus/"), ARG_STR(QFMT_VFS_OLD),
240 ARG_STR("/tmp/bogus/"));
241
242 snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
243 "QCMD(Q_QUOTAON, %#x /* ???QUOTA */)",
244 QCMD_TYPE(QCMD(Q_QUOTAON, 0xfacefeed)));
245 snprintf(invalid_id_str, sizeof(invalid_id_str),
246 "%#x /* QFMT_VFS_??? */", bogus_id);
247 check_quota(CQF_ID_STR, QCMD(Q_QUOTAON, 0xfacefeed),
248 invalid_cmd_str, bogus_dev, bogus_dev_str,
249 bogus_id, invalid_id_str, bogus_addr);
250
251
252 /* Q_QUOTAOFF */
253
254 check_quota(CQF_ID_SKIP | CQF_ADDR_SKIP,
255 ARG_STR(QCMD(Q_QUOTAOFF, USRQUOTA)),
256 bogus_special, bogus_special_str);
257 check_quota(CQF_ID_SKIP | CQF_ADDR_SKIP,
258 ARG_STR(QCMD(Q_QUOTAOFF, GRPQUOTA)),
259 ARG_STR("/dev/bogus/"));
260 check_quota(CQF_ID_SKIP | CQF_ADDR_SKIP,
261 ARG_STR(QCMD(Q_QUOTAOFF, PRJQUOTA)), ARG_STR(NULL));
262 check_quota(CQF_ID_SKIP | CQF_ADDR_SKIP,
263 QCMD(Q_QUOTAOFF, 3), "QCMD(Q_QUOTAOFF, 0x3 /* ???QUOTA */)",
264 ARG_STR(NULL));
265
266
267 /* Q_GETQUOTA */
268
269 /* Trying our best to get successful result */
270 check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_GETQUOTA, USRQUOTA)),
271 ARG_STR("/dev/sda1"), getuid(), dqblk, print_dqblk,
272 (intptr_t) 1);
273
274 check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_GETQUOTA, GRPQUOTA)),
275 ARG_STR(NULL), -1, dqblk, print_dqblk, (intptr_t) 1);
276
277
278 /* Q_GETNEXTQUOTA */
279
280 check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_GETNEXTQUOTA, USRQUOTA)),
281 ARG_STR("/dev/sda1"), 0, nextdqblk, print_nextdqblk,
282 (intptr_t) 1);
283
284
285 /* Q_SETQUOTA */
286
287 fill_memory(dqblk, sizeof(*dqblk));
288
289 check_quota(CQF_NONE, ARG_STR(QCMD(Q_SETQUOTA, PRJQUOTA)),
290 bogus_special, bogus_special_str, 0, bogus_addr);
291
292 check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_SETQUOTA, PRJQUOTA)),
293 ARG_STR("/dev/bogus/"), 3141592653U, dqblk, print_dqblk,
294 (intptr_t) 0);
295
296
297 /* Q_GETINFO */
298
299 check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
300 ARG_STR(QCMD(Q_GETINFO, GRPQUOTA)),
301 ARG_STR("/dev/sda1"), dqinfo, print_dqinfo, (intptr_t) 1);
302
303 check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
304 ARG_STR(QCMD(Q_GETINFO, GRPQUOTA)),
305 bogus_special, bogus_special_str, dqinfo,
306 print_dqinfo, (intptr_t) 1);
307
308 /* Q_SETINFO */
309
310 fill_memory(dqinfo, sizeof(*dqinfo));
311 /* In order to check flag printing correctness */
312 dqinfo->dqi_flags = 0xdeadabcd;
313
314 check_quota(CQF_ID_SKIP | CQF_ADDR_STR,
315 ARG_STR(QCMD(Q_SETINFO, PRJQUOTA)),
316 bogus_special, bogus_special_str, ARG_STR(NULL));
317
318 check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
319 ARG_STR(QCMD(Q_SETINFO, USRQUOTA)),
320 ARG_STR("/dev/bogus/"), dqinfo, print_dqinfo, (intptr_t) 0);
321
322
323 /* Q_GETFMT */
324
325 check_quota(CQF_ID_SKIP | CQF_ADDR_STR,
326 ARG_STR(QCMD(Q_GETFMT, PRJQUOTA)),
327 bogus_special, bogus_special_str, ARG_STR(NULL));
328 check_quota(CQF_ID_SKIP,
329 ARG_STR(QCMD(Q_GETFMT, USRQUOTA)),
330 unterminated, unterminated_str, fmt + 1);
331 check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
332 ARG_STR(QCMD(Q_GETFMT, GRPQUOTA)),
333 ARG_STR("/dev/sda1"), fmt, print_dqfmt, (uintptr_t) 1);
334 /* Try to check valid quota format */
335 *fmt = QFMT_VFS_OLD;
336 check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
337 ARG_STR(QCMD(Q_GETFMT, GRPQUOTA)),
338 ARG_STR("/dev/sda1"), fmt, print_dqfmt, (uintptr_t) 1);
339
340
341 /* Q_SYNC */
342
343 check_quota(CQF_ID_SKIP | CQF_ADDR_SKIP,
344 ARG_STR(QCMD(Q_SYNC, USRQUOTA)),
345 bogus_special, bogus_special_str);
346 check_quota(CQF_ID_SKIP | CQF_ADDR_SKIP,
347 QCMD(Q_SYNC, 0xfff), "QCMD(Q_SYNC, 0xff /* ???QUOTA */)",
348 ARG_STR(NULL));
349
350 puts("+++ exited with 0 +++");
351
352 return 0;
353 }
354
355 #else
356
357 SKIP_MAIN_UNDEFINED("__NR_quotactl && "
358 "(HAVE_LINUX_QUOTA_H || HAVE_SYS_QUOTA_H)");
359
360 #endif
361