Skip to contentSkip to conversation

ip route: check ftell, fseek return value

PATCH Superseded Hangbin Liu 4 participants 4 messages Sep 6, 2016 – Sep 7, 2016

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ip route: check ftell, fseek return value

ftell() may return -1 in error case, which is not handled and therefore pass a negative offset to fseek(). The return code of fseek() is also not checked.

---
 ip/iproute.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
ip/iproute.c +9 -2
index 3da23af..ba877dc 100644
@@ -1859,7 +1859,11 @@ static int iproute_restore(void)
 	if (route_dump_check_magic())
 		exit(-1);
 
-	pos = ftell(stdin);
+	if ((pos = ftell(stdin)) == -1) {
+		perror("Failed to restore: ftell");
+		exit(errno);
+	}
+
 	for (prio = 0; prio < 3; prio++) {
 		int err;
 
@@ -1867,7 +1871,10 @@ static int iproute_restore(void)
 		if (err)
 			exit(err);
 
-		fseek(stdin, pos, SEEK_SET);
+		if (fseek(stdin, pos, SEEK_SET) == -1) {
+			perror("Failed to restore: fseek");
+			exit(errno);
+		}
 	}
 
 	exit(0);
Signature · 1 line
2.5.5
#2 lore raw
In reply to Hangbin Liu

From: Hangbin Liu

Quoted 15 lines

Sent: 06 September 2016 07:40
ftell() may return -1 in error case, which is not handled and therefore pass a negative offset to fseek(). The return code of fseek() is also not checked.

---
 ip/iproute.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
ip/iproute.c +0 -0
index 3da23af..ba877dc 100644

ip/iproute.c static int iproute_restore(void) · line 1862

Quoted 5 lines
ip/iproute.c +1 -1
@@ -1859,7 +1859,11 @@ static int iproute_restore(void)
 	if (route_dump_check_magic())
 		exit(-1);
 
-	pos = ftell(stdin);
+	if ((pos = ftell(stdin)) == -1) {

Don't put assignments in conditionals.

ip/iproute.c static int iproute_restore(void) · line 1864

Quoted 2 lines
ip/iproute.c +2 -0
+		perror("Failed to restore: ftell");
+		exit(errno);

errno is not a valid argument to exit().

...

Actually WTF is this code trying to do.
stdin is very likely to be a pipe, so expecting to seek on it seems very likely to fail.

David

#3 lore raw
Quoted 24 lines · David Laight

From: Hangbin Liu

Quoted 15 lines

Sent: 06 September 2016 07:40
ftell() may return -1 in error case, which is not handled and therefore pass a negative offset to fseek(). The return code of fseek() is also not checked.

---
 ip/iproute.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
ip/iproute.c +0 -0
index 3da23af..ba877dc 100644

ip/iproute.c static int iproute_restore(void) · line 1862

Quoted 5 lines
ip/iproute.c +1 -1
@@ -1859,7 +1859,11 @@ static int iproute_restore(void)
 	if (route_dump_check_magic())
 		exit(-1);
 
-	pos = ftell(stdin);
+	if ((pos = ftell(stdin)) == -1) {

Don't put assignments in conditionals.

Quoted 4 lines

ip/iproute.c static int iproute_restore(void) · line 1864

Quoted 2 lines
ip/iproute.c +2 -0
+		perror("Failed to restore: ftell");
+		exit(errno);

errno is not a valid argument to exit().

Hi David,

Thanks for the advice. I will send PATCHv2 to fix them.

Quoted 6 lines

...

Actually WTF is this code trying to do.
stdin is vere likely to be a pipe, so expecting to seek on it seems very likely to fail.

I will fix the return code handle issue first and try investigate the pipe issue later.

Regards
Hangbin