[PATCH v1 3/4] tests: improve quotactl coverage

Zhibin Li 08826794brmt at gmail.com
Sun Jul 8 16:11:31 UTC 2018


Use check_quota function for invalid commands instead of manually syscall invocation.
In this way injection can also be checked for these commands in the next commit.

* tests/quotactl.c: (print_dqfmt): New function.
(invalid_cmd_str, invalid_id_str): New variables.
* tests/quotactl-xfs.c: Removed unused variable rc.
(invalid_cmd): New variable.
---
 tests/quotactl-xfs.c | 35 +++++++++++++++---------
 tests/quotactl.c     | 76 +++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 80 insertions(+), 31 deletions(-)

diff --git a/tests/quotactl-xfs.c b/tests/quotactl-xfs.c
index 036b61d2..c08b1584 100644
--- a/tests/quotactl-xfs.c
+++ b/tests/quotactl-xfs.c
@@ -207,7 +207,7 @@ main(void)
 	char bogus_addr_str[sizeof(void *) * 2 + sizeof("0x")];
 	char unterminated_str[sizeof(void *) * 2 + sizeof("0x")];
 
-	long rc;
+	static char invalid_cmd[1024];
 	TAIL_ALLOC_OBJECT_CONST_PTR(struct fs_disk_quota, xdq);
 	TAIL_ALLOC_OBJECT_CONST_PTR(struct fs_quota_stat, xqstat);
 	TAIL_ALLOC_OBJECT_CONST_PTR(struct fs_quota_statv, xqstatv);
@@ -234,12 +234,12 @@ main(void)
 		    "|XFS_QUOTA_GDQ_ACCT|XFS_QUOTA_GDQ_ENFD"
 		    "|XFS_QUOTA_PDQ_ENFD|0xdeadbec0]");
 
-	rc = syscall(__NR_quotactl, QCMD(Q_XQUOTAON, 0xfacefeed), bogus_dev,
-		     bogus_id, bogus_addr);
-	printf("quotactl(QCMD(Q_XQUOTAON, %#x /* ???QUOTA */)"
-	       ", %s, %p) = %s\n",
-	       QCMD_TYPE(QCMD(Q_XQUOTAON, 0xfacefeed)),
-	       bogus_dev_str, bogus_addr, sprintrc(rc));
+	snprintf(invalid_cmd, sizeof(invalid_cmd),
+		 "QCMD(Q_XQUOTAON, %#x /* ???QUOTA */)",
+		 QCMD_TYPE(QCMD(Q_XQUOTAON, 0xfacefeed)));
+	check_quota(CQF_ID_SKIP,
+		    QCMD(Q_XQUOTAON, 0xfacefeed), invalid_cmd,
+		    bogus_dev, bogus_dev_str, bogus_addr);
 
 
 	/* Q_XQUOTAOFF */
@@ -264,11 +264,11 @@ main(void)
 	/* Q_XGETQUOTA */
 
 	/* Trying our best to get successful result */
-	check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_GETQUOTA, USRQUOTA)),
+	check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_XGETQUOTA, USRQUOTA)),
 		    ARG_STR("/dev/sda1"), getuid(), xdq, print_xdisk_quota,
 		    (intptr_t) 1);
 
-	check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_GETQUOTA, GRPQUOTA)),
+	check_quota(CQF_ADDR_CB, ARG_STR(QCMD(Q_XGETQUOTA, GRPQUOTA)),
 		    ARG_STR(NULL), -1, xdq, print_xdisk_quota, (intptr_t) 2);
 
 
@@ -298,21 +298,30 @@ main(void)
 		    ARG_STR("/dev/sda1"), xqstat, print_xquota_stat, (intptr_t) 1);
 
 	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
-		    ARG_STR(QCMD(Q_XGETQSTATV, PRJQUOTA)),
+		    ARG_STR(QCMD(Q_XGETQSTAT, USRQUOTA)),
+		    ARG_STR("NULL"), xqstat, print_xquota_stat, (intptr_t) 2);
+
+	check_quota(CQF_ID_SKIP,
+		    ARG_STR(QCMD(Q_XGETQSTAT, PRJQUOTA)),
 		    unterminated, unterminated_str,
-		    xqstat + 1, print_xquota_stat, (intptr_t) 2);
+		    xqstat + 1);
 
 
 	/* Q_XGETQSTATV */
 
 	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
-		    ARG_STR(QCMD(Q_XGETQSTAT, USRQUOTA)),
-		    ARG_STR("/dev/sda1"), xqstatv, print_xquota_statv, 1);
+		    ARG_STR(QCMD(Q_XGETQSTATV, USRQUOTA)),
+		    ARG_STR("/dev/sda1"), xqstatv, print_xquota_statv, (intptr_t) 1);
 
 	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
 		    ARG_STR(QCMD(Q_XGETQSTATV, GRPQUOTA)),
 		    ARG_STR(NULL), xqstatv, print_xquota_statv, (intptr_t) 2);
 
+	check_quota(CQF_ID_SKIP,
+		    ARG_STR(QCMD(Q_XGETQSTATV, PRJQUOTA)),
+		    unterminated, unterminated_str,
+		    xqstatv + 1);
+
 
 	/* Q_XQUOTARM */
 
diff --git a/tests/quotactl.c b/tests/quotactl.c
index f1648d94..67aa936f 100644
--- a/tests/quotactl.c
+++ b/tests/quotactl.c
@@ -161,6 +161,38 @@ print_dqinfo(long rc, void *ptr, void *arg)
 	printf("}");
 }
 
+void
+print_dqfmt(long rc, void *ptr, void *arg)
+{
+	uint32_t *fmtval = ptr;
+	long out_arg = (long) arg;
+	const char *fmtstr;
+
+	if (((rc != 0) && out_arg) || (out_arg > 1)) {
+		printf("%p", fmtval);
+		return;
+	}
+	printf("[");
+	switch (*fmtval) {
+	case 1:
+		fmtstr = "QFMT_VFS_OLD";
+		break;
+	case 2:
+		fmtstr = "QFMT_VFS_V0";
+		break;
+	case 3:
+		fmtstr = "QFMT_OCFS2";
+		break;
+	case 4:
+		fmtstr = "QFMT_VFS_V1";
+		break;
+	default:
+		printf("%#x /* QFMT_VFS_??? */]", *fmtval);
+		return;
+	}
+	printf("%s]", fmtstr);
+}
+
 
 int
 main(void)
@@ -171,7 +203,6 @@ main(void)
 	char bogus_special_str[sizeof(void *) * 2 + sizeof("0x")];
 	char unterminated_str[sizeof(void *) * 2 + sizeof("0x")];
 
-	long rc;
 	char *unterminated = tail_memdup(unterminated_data,
 					 sizeof(unterminated_data));
 	TAIL_ALLOC_OBJECT_CONST_PTR(struct if_dqblk, dqblk);
@@ -179,6 +210,8 @@ main(void)
 	TAIL_ALLOC_OBJECT_CONST_PTR(uint32_t, fmt);
 	TAIL_ALLOC_OBJECT_CONST_PTR(struct if_nextdqblk, nextdqblk);
 
+	static char invalid_cmd_str[1024];
+	static char invalid_id_str[1024];
 
 	snprintf(bogus_special_str, sizeof(bogus_special_str), "%p",
 		bogus_special);
@@ -188,16 +221,16 @@ main(void)
 
 	/* Invalid commands */
 
-	rc = syscall(__NR_quotactl, bogus_cmd, bogus_special, bogus_id,
-		     bogus_addr);
-	printf("quotactl(QCMD(%#x /* Q_??? */, %#x /* ???QUOTA */)"
-	       ", %p, %u, %p) = %s\n",
-	       QCMD_CMD(bogus_cmd), QCMD_TYPE(bogus_cmd),
-	       bogus_special, bogus_id, bogus_addr, sprintrc(rc));
+	snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
+		 "QCMD(%#x /* Q_??? */, %#x /* ???QUOTA */)",
+		 QCMD_CMD(bogus_cmd), QCMD_TYPE(bogus_cmd));
+	check_quota(CQF_NONE, bogus_cmd, invalid_cmd_str,
+		    bogus_special, bogus_special_str, bogus_id, bogus_addr);
 
-	rc = syscall(__NR_quotactl, 0, NULL, -1, NULL);
-	printf("quotactl(QCMD(0 /* Q_??? */, USRQUOTA), NULL, -1, NULL) = %s\n",
-	       sprintrc(rc));
+	snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
+		 "QCMD(0 /* Q_??? */, USRQUOTA)");
+	check_quota(CQF_ADDR_STR, 0, invalid_cmd_str,
+		    ARG_STR(NULL), -1, ARG_STR(NULL));
 
 
 	/* Q_QUOTAON */
@@ -207,12 +240,14 @@ main(void)
 		    ARG_STR("/dev/bogus/"), ARG_STR(QFMT_VFS_OLD),
 		    ARG_STR("/tmp/bogus/"));
 
-	rc = syscall(__NR_quotactl, QCMD(Q_QUOTAON, 0xfacefeed), bogus_dev,
-		     bogus_id, bogus_addr);
-	printf("quotactl(QCMD(Q_QUOTAON, %#x /* ???QUOTA */)"
-	       ", %s, %#x /* QFMT_VFS_??? */, %p) = %s\n",
-	       QCMD_TYPE(QCMD(Q_QUOTAON, 0xfacefeed)),
-	       bogus_dev_str, bogus_id, bogus_addr, sprintrc(rc));
+	snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
+		 "QCMD(Q_QUOTAON, %#x /* ???QUOTA */)",
+		 QCMD_TYPE(QCMD(Q_QUOTAON, 0xfacefeed)));
+	snprintf(invalid_id_str, sizeof(invalid_id_str),
+		 "%#x /* QFMT_VFS_??? */", bogus_id);
+	check_quota(CQF_ID_STR, QCMD(Q_QUOTAON, 0xfacefeed),
+		    invalid_cmd_str, bogus_dev, bogus_dev_str,
+		    bogus_id, invalid_id_str, bogus_addr);
 
 
 	/* Q_QUOTAOFF */
@@ -294,9 +329,14 @@ main(void)
 	check_quota(CQF_ID_SKIP,
 		    ARG_STR(QCMD(Q_GETFMT, USRQUOTA)),
 		    unterminated, unterminated_str, fmt + 1);
-	check_quota(CQF_ID_SKIP,
+	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
+		    ARG_STR(QCMD(Q_GETFMT, GRPQUOTA)),
+		    ARG_STR("/dev/sda1"), fmt, print_dqfmt, (uintptr_t) 1);
+	/* Try to check valid quota format */
+	*fmt = QFMT_VFS_OLD;
+	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
 		    ARG_STR(QCMD(Q_GETFMT, GRPQUOTA)),
-		    ARG_STR("/dev/sda1"), fmt);
+		    ARG_STR("/dev/sda1"), fmt, print_dqfmt, (uintptr_t) 1);
 
 
 	/* Q_SYNC */
-- 
2.14.4



More information about the Strace-devel mailing list