Skip to contentSkip to conversation

setsockopt08: Handle ENOPROTOOPT even with compatible config

PATCH Superseded Richard Palethorpe 3 participants 5 messages Aug 6, 2021 – Aug 11, 2021

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 setsockopt08: Handle ENOPROTOOPT even with compatible config

One or more necessary modules can be missing even if they are present in the config.

---
 .../kernel/syscalls/setsockopt/setsockopt08.c | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt08.c +20 -4
index 33892f9b1..d3cd5b5b2 100644
@@ -110,6 +110,7 @@ void run(void)
 	struct xt_entry_target *xt_entry_tgt =
 		((struct xt_entry_target *) (&ipt_entry->elems[0] + match_size));
 	int fd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	int result;
 
 	xt_entry_match->u.user.match_size = (u_int16_t)match_size;
 	strcpy(xt_entry_match->u.user.name, "state");
@@ -126,10 +127,25 @@ void run(void)
 	ipt_replace->num_counters = 1;
 	ipt_replace->size = ipt_entry->next_offset;
 
-	TST_EXP_FAIL(setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1),
-		     EINVAL,
-		     "setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)",
-		     fd, buffer);
+	errno = 0;
+	if (setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1) == -1) {
+		switch (errno) {
+		case EINVAL:
+			result = TPASS;
+			break;
+		case ENOPROTOOPT:
+			result = TCONF;
+			break;
+		default:
+			result = TFAIL;
+		}
+	} else {
+		result = TFAIL;
+	}
+
+	tst_res(result | TERRNO,
+		"setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)",
+		fd, buffer);
 
 	SAFE_CLOSE(fd);
 }
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/1 setsockopt08: Handle ENOPROTOOPT even with compatible config

Hi,

Quoted 40 lines · Richard Palethorpe via ltp

One or more necessary modules can be missing even if they are present in the config.

---
 .../kernel/syscalls/setsockopt/setsockopt08.c | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt08.c +13 -4
index 33892f9b1..d3cd5b5b2 100644
@@ -110,6 +110,7 @@ void run(void)
 	struct xt_entry_target *xt_entry_tgt =
 		((struct xt_entry_target *) (&ipt_entry->elems[0] + match_size));
 	int fd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	int result;
 
 	xt_entry_match->u.user.match_size = (u_int16_t)match_size;
 	strcpy(xt_entry_match->u.user.name, "state");
@@ -126,10 +127,25 @@ void run(void)
 	ipt_replace->num_counters = 1;
 	ipt_replace->size = ipt_entry->next_offset;
 
-	TST_EXP_FAIL(setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1),
-		     EINVAL,
-		     "setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)",
-		     fd, buffer);
+	errno = 0;
+	if (setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1) == -1) {
+		switch (errno) {
+		case EINVAL:
+			result = TPASS;
+			break;
+		case ENOPROTOOPT:
+			result = TCONF;
+			break;
+		default:
+			result = TFAIL;
+		}

testcases/kernel/syscalls/setsockopt/setsockopt08.c void run(void) · line 151

Quoted 10 lines
testcases/kernel/syscalls/setsockopt/setsockopt08.c +7 -0
+	} else {
+		result = TFAIL;
+	}
+
+	tst_res(result | TERRNO,
+		"setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)",
+		fd, buffer);
 
 	SAFE_CLOSE(fd);
 }

I think it'd be better to call tst_brk(TCONF) when setsockopt() returns ENOPROTOOPT. The CVE runfile iterates the test 100 times and this error will not change between iterations.

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#2 lore raw

PATCH 1/1 setsockopt08: Handle ENOPROTOOPT even with compatible config

Hello Martin,

Quoted 51 lines · Martin Doucha

Hi,

Quoted 48 lines · Richard Palethorpe via ltp

One or more necessary modules can be missing even if they are present in the config.

---
 .../kernel/syscalls/setsockopt/setsockopt08.c | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt08.c +20 -4
index 33892f9b1..d3cd5b5b2 100644
@@ -110,6 +110,7 @@ void run(void)
 	struct xt_entry_target *xt_entry_tgt =
 		((struct xt_entry_target *) (&ipt_entry->elems[0] + match_size));
 	int fd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	int result;
 
 	xt_entry_match->u.user.match_size = (u_int16_t)match_size;
 	strcpy(xt_entry_match->u.user.name, "state");
@@ -126,10 +127,25 @@ void run(void)
 	ipt_replace->num_counters = 1;
 	ipt_replace->size = ipt_entry->next_offset;
 
-	TST_EXP_FAIL(setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1),
-		     EINVAL,
-		     "setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)",
-		     fd, buffer);
+	errno = 0;
+	if (setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1) == -1) {
+		switch (errno) {
+		case EINVAL:
+			result = TPASS;
+			break;
+		case ENOPROTOOPT:
+			result = TCONF;
+			break;
+		default:
+			result = TFAIL;
+		}
+	} else {
+		result = TFAIL;
+	}
+
+	tst_res(result | TERRNO,
+		"setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)",
+		fd, buffer);
 
Quoted 6 lines

testcases/kernel/syscalls/setsockopt/setsockopt08.c void run(void) · line 151

Quoted 2 lines
testcases/kernel/syscalls/setsockopt/setsockopt08.c +0 -0
 	SAFE_CLOSE(fd);
 }

I think it'd be better to call tst_brk(TCONF) when setsockopt() returns ENOPROTOOPT. The CVE runfile iterates the test 100 times and this error will not change between iterations.

Ah, yes, this is a mistake, I will reroll the patch.

Signature · 2 lines
Thank you,
Richard.
#3 lore raw

PATCH v2 1/1 setsockopt08: Handle ENOPROTOOPT even with compatible config

In reply to Richard Palethorpe

One or more necessary modules can be missing even if they are present in the config.

---

V2:
* Use tst_brk with TCONF

 .../kernel/syscalls/setsockopt/setsockopt08.c | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt08.c +20 -4
index 33892f9b1..7afb98403 100644
@@ -101,6 +101,8 @@ void setup(void)
 
 void run(void)
 {
+	const char *const res_fmt_str =
+		"setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)";
 	struct ipt_replace *ipt_replace = buffer;
 	struct ipt_entry *ipt_entry = &ipt_replace->entries[0];
 	struct xt_entry_match *xt_entry_match =
@@ -110,6 +112,7 @@ void run(void)
 	struct xt_entry_target *xt_entry_tgt =
 		((struct xt_entry_target *) (&ipt_entry->elems[0] + match_size));
 	int fd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	int result;
 
 	xt_entry_match->u.user.match_size = (u_int16_t)match_size;
 	strcpy(xt_entry_match->u.user.name, "state");
@@ -126,10 +129,23 @@ void run(void)
 	ipt_replace->num_counters = 1;
 	ipt_replace->size = ipt_entry->next_offset;
 
-	TST_EXP_FAIL(setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1),
-		     EINVAL,
-		     "setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)",
-		     fd, buffer);
+	errno = 0;
+	if (setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1) == -1) {
+		switch (errno) {
+		case EINVAL:
+			result = TPASS;
+			break;
+		case ENOPROTOOPT:
+			tst_brk(TCONF | TERRNO, res_fmt_str, fd, buffer);
+			return;
+		default:
+			result = TFAIL;
+		}
+	} else {
+		result = TFAIL;
+	}
+
+	tst_res(result | TERRNO, res_fmt_str, fd, buffer);
 
 	SAFE_CLOSE(fd);
 }
Signature · 1 line
2.31.1
#4 lore raw

PATCH v2 1/1 setsockopt08: Handle ENOPROTOOPT even with compatible config

Quoted 52 lines · Richard Palethorpe via ltp

One or more necessary modules can be missing even if they are present in the config.

---

V2:
* Use tst_brk with TCONF

 .../kernel/syscalls/setsockopt/setsockopt08.c | 24 +++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
testcases/kernel/syscalls/setsockopt/setsockopt08.c +13 -4
index 33892f9b1..7afb98403 100644
@@ -101,6 +101,8 @@ void setup(void)
 
 void run(void)
 {
+	const char *const res_fmt_str =
+		"setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)";
 	struct ipt_replace *ipt_replace = buffer;
 	struct ipt_entry *ipt_entry = &ipt_replace->entries[0];
 	struct xt_entry_match *xt_entry_match =
@@ -110,6 +112,7 @@ void run(void)
 	struct xt_entry_target *xt_entry_tgt =
 		((struct xt_entry_target *) (&ipt_entry->elems[0] + match_size));
 	int fd = SAFE_SOCKET(AF_INET, SOCK_DGRAM, 0);
+	int result;
 
 	xt_entry_match->u.user.match_size = (u_int16_t)match_size;
 	strcpy(xt_entry_match->u.user.name, "state");
@@ -126,10 +129,23 @@ void run(void)
 	ipt_replace->num_counters = 1;
 	ipt_replace->size = ipt_entry->next_offset;
 
-	TST_EXP_FAIL(setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1),
-		     EINVAL,
-		     "setsockopt(%d, IPPROTO_IP, IPT_SO_SET_REPLACE, %p, 1)",
-		     fd, buffer);
+	errno = 0;
+	if (setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1) == -1) {
+		switch (errno) {
+		case EINVAL:
+			result = TPASS;
+			break;
+		case ENOPROTOOPT:
+			tst_brk(TCONF | TERRNO, res_fmt_str, fd, buffer);
+			return;
+		default:

testcases/kernel/syscalls/setsockopt/setsockopt08.c void run(void) · line 151

Quoted 10 lines
testcases/kernel/syscalls/setsockopt/setsockopt08.c +7 -0
+			result = TFAIL;
+		}
+	} else {
+		result = TFAIL;
+	}
+
+	tst_res(result | TERRNO, res_fmt_str, fd, buffer);
 
 	SAFE_CLOSE(fd);
 }

I think this would be much cleaner:

TEST(setsockopt(fd, IPPROTO_IP, IPT_SO_SET_REPLACE, buffer, 1));

if (TST_RET == -1 && TST_ERR == ENOPROTOOPT)

	tst_brk(TCONF | TTERRNO, res_fmt_str, fd, buffer);

result = (TST_RET == -1 && TST_ERR == EINVAL) ? TPASS : TFAIL; tst_res(result | TTERRNO, res_fmt_str, fd, buffer);

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic