Skip to contentSkip to conversation

Do not segfault because of kernel version

PATCH In review Luk Claes 4 participants 13 messages Jul 2, 2011 – Jul 12, 2011

Subsystems (2)
lore
Share & tools
lore

Luk Claes

author
#0 lore raw

PATCH 1/1 Do not segfault because of kernel version

mount.nfs segfaults if kernel version number does not contain at least 3 components delimited with a dot.

Avoid this by matching up to three unsigned integers inialised to zero, separated by dots.

---
 utils/mount/version.h |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)
utils/mount/version.h +5 -6
index af61a6f..2642eab 100644
@@ -23,8 +23,7 @@
 #ifndef _NFS_UTILS_MOUNT_VERSION_H
 #define _NFS_UTILS_MOUNT_VERSION_H
 
-#include <stdlib.h>
-#include <string.h>
+#include <stdio.h>
 
 #include <sys/utsname.h>
 
@@ -37,14 +36,14 @@ static inline unsigned int MAKE_VERSION(unsigned int p, unsigned int q,
 static inline unsigned int linux_version_code(void)
 {
 	struct utsname my_utsname;
-	unsigned int p, q, r;
+	unsigned int p, q = 0, r = 0;
 
 	if (uname(&my_utsname))
 		return 0;
 
-	p = (unsigned int)atoi(strtok(my_utsname.release, "."));
-	q = (unsigned int)atoi(strtok(NULL, "."));
-	r = (unsigned int)atoi(strtok(NULL, "."));
+	if (sscanf(my_utsname.release, "%u.%u.%u", &p, &q, &r) < 1)
+		return 0;
+	
 	return MAKE_VERSION(p, q, r);
 }
 
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 31 lines · On Sat, 2 Jul 2011 16:32:29 +0200 Luk Claes <[email protected]> wrote:

mount.nfs segfaults if kernel version number does not contain at least 3 components delimited with a dot.

Avoid this by matching up to three unsigned integers inialised to zero, separated by dots.

---
 utils/mount/version.h |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)
utils/mount/version.h +2 -3
index af61a6f..2642eab 100644
@@ -23,8 +23,7 @@
 #ifndef _NFS_UTILS_MOUNT_VERSION_H
 #define _NFS_UTILS_MOUNT_VERSION_H
 
-#include <stdlib.h>
-#include <string.h>
+#include <stdio.h>
 
 #include <sys/utsname.h>
 
@@ -37,14 +36,14 @@ static inline unsigned int MAKE_VERSION(unsigned int p, unsigned int q,
 static inline unsigned int linux_version_code(void)
 {
 	struct utsname my_utsname;
-	unsigned int p, q, r;
+	unsigned int p, q = 0, r = 0;

utils/mount/version.h static inline unsigned int MAKE_VERSION(unsigned int p, unsigned int q, · line 46

Quoted 10 lines
utils/mount/version.h +3 -3
 
 	if (uname(&my_utsname))
 		return 0;
 
-	p = (unsigned int)atoi(strtok(my_utsname.release, "."));
-	q = (unsigned int)atoi(strtok(NULL, "."));
-	r = (unsigned int)atoi(strtok(NULL, "."));
+	if (sscanf(my_utsname.release, "%u.%u.%u", &p, &q, &r) < 1)
+		return 0;
+	

According to Linus, the error case should return a big number, not a small number.
So if the version number is not recognised - e.g. it was written in Sanskrit rather than arabic numberals, it is assumed to be a future version, not a past version.

https://lkml.org/lkml/2011/6/14/293

NeilBrown

Quoted 3 lines
utils/mount/version.h +0 -0
 	return MAKE_VERSION(p, q, r);
 }
 
author
#2 lore raw
Quoted 50 lines · NeilBrown
Quoted 31 lines · On Sat, 2 Jul 2011 16:32:29 +0200 Luk Claes <[email protected]> wrote:

mount.nfs segfaults if kernel version number does not contain at least 3 components delimited with a dot.

Avoid this by matching up to three unsigned integers inialised to zero, separated by dots.

---
 utils/mount/version.h |   11 +++++------
 1 files changed, 5 insertions(+), 6 deletions(-)
utils/mount/version.h +2 -3
index af61a6f..2642eab 100644
@@ -23,8 +23,7 @@
 #ifndef _NFS_UTILS_MOUNT_VERSION_H
 #define _NFS_UTILS_MOUNT_VERSION_H
 
-#include <stdlib.h>
-#include <string.h>
+#include <stdio.h>
 
 #include <sys/utsname.h>
 
@@ -37,14 +36,14 @@ static inline unsigned int MAKE_VERSION(unsigned int p, unsigned int q,
 static inline unsigned int linux_version_code(void)
 {
 	struct utsname my_utsname;
-	unsigned int p, q, r;
+	unsigned int p, q = 0, r = 0;

utils/mount/version.h static inline unsigned int MAKE_VERSION(unsigned int p, unsigned int q, · line 46

Quoted 10 lines
utils/mount/version.h +3 -3
 
 	if (uname(&my_utsname))
 		return 0;
 
-	p = (unsigned int)atoi(strtok(my_utsname.release, "."));
-	q = (unsigned int)atoi(strtok(NULL, "."));
-	r = (unsigned int)atoi(strtok(NULL, "."));
+	if (sscanf(my_utsname.release, "%u.%u.%u", &p, &q, &r) < 1)
+		return 0;
+	

According to Linus, the error case should return a big number, not a small number.
So if the version number is not recognised - e.g. it was written in Sanskrit rather than arabic numberals, it is assumed to be a future version, not a past version.

Quoted 1 line

https://lkml.org/lkml/2011/6/14/293

Ah, not a bad idea. In that case a parse error can be distinguished from other errors. Would 'return -1' do the expected or will it give a compiler warning/error we should avoid?

Cheers

Luk

#3 lore raw
Quoted 5 lines · Luk Claes

So if the version number is not recognised - e.g. it was written in Sanskrit rather than arabic numberals, it is assumed to be a future version, not a past version.

https://lkml.org/lkml/2011/6/14/293

Ah, not a bad idea. In that case a parse error can be distinguished from other errors. Would 'return -1' do the expected or will it give a compiler warning/error we should avoid?

You can't return -1 from a function returning unsigned int. I think you want to return something like

MAKE_VERSION(9999, 255, 255)

author
#4 lore raw
Quoted 8 lines · Jim Rees
Quoted 5 lines · Luk Claes

So if the version number is not recognised - e.g. it was written in Sanskrit rather than arabic numberals, it is assumed to be a future version, not a past version.

https://lkml.org/lkml/2011/6/14/293

Quoted 8 lines

Ah, not a bad idea. In that case a parse error can be distinguished from other errors. Would 'return -1' do the expected or will it give a compiler warning/error we should avoid?

You can't return -1 from a function returning unsigned int. I think you want to return something like

MAKE_VERSION(9999, 255, 255)

Would it not be better to return UINT_MAX in that case to avoid having to change it when version 10000 would be released and to avoid overflows that could potentially order lower?

Cheers

Luk

#5 lore raw
Quoted 4 lines · Luk Claes

You can't return -1 from a function returning unsigned int. I think you want to return something like

MAKE_VERSION(9999, 255, 255)

Would it not be better to return UINT_MAX in that case to avoid having to change it when version 10000 would be released and to avoid overflows that could potentially order lower?

Maybe. I wanted the second and third numbers to be the max possible (255). But of course they will be anyway if you return UINT_MAX and are running on an architecture that represents ints in two's complement binary. Which is the case today, but wasn't there a port of unix to the System 36 at one time? Ok, that's just silly.

Yes, just return UINT_MAX. Fix the other error return too, the one where uname fails. And put in a comment if you can briefly summarize Linus's argument.

author
#6 lore raw
Quoted 17 lines · Jim Rees
Quoted 4 lines · Luk Claes

You can't return -1 from a function returning unsigned int. I think you want to return something like

MAKE_VERSION(9999, 255, 255)

Would it not be better to return UINT_MAX in that case to avoid having to change it when version 10000 would be released and to avoid overflows that could potentially order lower?

Maybe. I wanted the second and third numbers to be the max possible (255). But of course they will be anyway if you return UINT_MAX and are running on an architecture that represents ints in two's complement binary. Which is the case today, but wasn't there a port of unix to the System 36 at one time? Ok, that's just silly.

Quoted 3 lines

Yes, just return UINT_MAX. Fix the other error return too, the one where uname fails. And put in a comment if you can briefly summarize Linus's argument.

I thought that a real error like uname failing should still get the 'wrong' return 0, no?

Cheers

Luk

#7 lore raw
Quoted 3 lines · Luk Claes

Yes, just return UINT_MAX. Fix the other error return too, the one where uname fails. And put in a comment if you can briefly summarize Linus's argument.

I thought that a real error like uname failing should still get the 'wrong' return 0, no?

No. As I read it, Linus argues that you should only run the backward compatibility code path when you know you're running an older kernel. If you don't know, then you should assume you're running a newer kernel.

author
#8 lore raw
Quoted 6 lines · Jim Rees
Quoted 3 lines · Luk Claes

Yes, just return UINT_MAX. Fix the other error return too, the one where uname fails. And put in a comment if you can briefly summarize Linus's argument.

Quoted 6 lines

I thought that a real error like uname failing should still get the 'wrong' return 0, no?

No. As I read it, Linus argues that you should only run the backward compatibility code path when you know you're running an older kernel. If you don't know, then you should assume you're running a newer kernel.

So, if uname fails we treat it as a newer kernel, shouldn't we treat that as an error? So treating it as a special case instead of running backward compatibility or as a newer kernel?

Cheers

Luk

#9 lore raw

Luk Claes wrote:

Quoted 6 lines · Jim Rees
Quoted 3 lines · Luk Claes

Yes, just return UINT_MAX. Fix the other error return too, the one where uname fails. And put in a comment if you can briefly summarize Linus's argument.

Quoted 6 lines

I thought that a real error like uname failing should still get the 'wrong' return 0, no?

No. As I read it, Linus argues that you should only run the backward compatibility code path when you know you're running an older kernel. If you don't know, then you should assume you're running a newer kernel.

So, if uname fails we treat it as a newer kernel, shouldn't we treat that as an error?

That would seem wrong to me. The current code does not treat it as an error, so we're breaking something that used to work. And failing the mount because uname won't run seems unreasonable. That could happen at the worst possible time, for example on a system where the local disk has become corrupt and you're fixing it from a rescue CD.

author
#10 lore raw

Resending patch taking into account the remarks of Neil Brown and Jim Rees.

Changes to previous attempt:

* Using MAX_UINT for versions that do not start with an integer
* Using MAX_UINT when uname does not work

author
#11 lore raw

PATCH 1/1 Do not segfault because of kernel version

mount.nfs segfaults if kernel version number does not contain at least 3 components delimited with a dot.

Avoid this by matching up to three unsigned integers inialised to zero, separated by dots.

A version that does not start with an integer is probably a future version where the versioning evolved to another scheme. Return UINT_MAX which is guaranteed to be higher than existing versions. This would also make it possible to easily identify versions that do not start with an integer.

---
 utils/mount/version.h |   16 +++++++++-------
 1 files changed, 9 insertions(+), 7 deletions(-)
utils/mount/version.h +9 -7
index af61a6f..531cf68 100644
@@ -23,8 +23,8 @@
 #ifndef _NFS_UTILS_MOUNT_VERSION_H
 #define _NFS_UTILS_MOUNT_VERSION_H
 
-#include <stdlib.h>
-#include <string.h>
+#include <stdio.h>
+#include <limits.h>
 
 #include <sys/utsname.h>
 
@@ -37,14 +37,16 @@ static inline unsigned int MAKE_VERSION(unsigned int p, unsigned int q,
 static inline unsigned int linux_version_code(void)
 {
 	struct utsname my_utsname;
-	unsigned int p, q, r;
+	unsigned int p, q = 0, r = 0;
 
+	/* UINT_MAX as backward compatibility code should not be run */
 	if (uname(&my_utsname))
-		return 0;
+		return UINT_MAX;
 
-	p = (unsigned int)atoi(strtok(my_utsname.release, "."));
-	q = (unsigned int)atoi(strtok(NULL, "."));
-	r = (unsigned int)atoi(strtok(NULL, "."));
+	/* UINT_MAX as future versions might not start with an integer */
+	if (sscanf(my_utsname.release, "%u.%u.%u", &p, &q, &r) < 1)
+		return UINT_MAX;
+	
 	return MAKE_VERSION(p, q, r);
 }
 
Signature · 1 line
1.7.5.4
#12 lore raw
Quoted 6 lines · Luk Claes

mount.nfs segfaults if kernel version number does not contain at least 3 components delimited with a dot.

Avoid this by matching up to three unsigned integers inialised to zero, separated by dots.

Quoted 7 lines

A version that does not start with an integer is probably a future version where the versioning evolved to another scheme. Return UINT_MAX which is guaranteed to be higher than existing versions. This would also make it possible to easily identify versions that do not start with an integer.

Committed....

steved.

Quoted 41 lines
---
 utils/mount/version.h |   16 +++++++++-------
 1 files changed, 9 insertions(+), 7 deletions(-)
utils/mount/version.h +9 -7
index af61a6f..531cf68 100644
@@ -23,8 +23,8 @@
 #ifndef _NFS_UTILS_MOUNT_VERSION_H
 #define _NFS_UTILS_MOUNT_VERSION_H
 
-#include <stdlib.h>
-#include <string.h>
+#include <stdio.h>
+#include <limits.h>
 
 #include <sys/utsname.h>
 
@@ -37,14 +37,16 @@ static inline unsigned int MAKE_VERSION(unsigned int p, unsigned int q,
 static inline unsigned int linux_version_code(void)
 {
 	struct utsname my_utsname;
-	unsigned int p, q, r;
+	unsigned int p, q = 0, r = 0;
 
+	/* UINT_MAX as backward compatibility code should not be run */
 	if (uname(&my_utsname))
-		return 0;
+		return UINT_MAX;
 
-	p = (unsigned int)atoi(strtok(my_utsname.release, "."));
-	q = (unsigned int)atoi(strtok(NULL, "."));
-	r = (unsigned int)atoi(strtok(NULL, "."));
+	/* UINT_MAX as future versions might not start with an integer */
+	if (sscanf(my_utsname.release, "%u.%u.%u", &p, &q, &r) < 1)
+		return UINT_MAX;
+	
 	return MAKE_VERSION(p, q, r);
 }