libsemanage: get_home_dirs: cleanup parsing of values from conf files
PATCH 1/1 libsemanage: get_home_dirs: cleanup parsing of values from conf files
atoi (3) is... bugprone. It's virtually impossible to differentiate an invalid value (e.g. the string "foo") from a valid value such as "0" as 0 is returned on error! From the manual page:
Quoted 3 lines
except that
atoi()does not detect errors.
RETURN VALUE
The converted value or 0 on error.
In the case of get_home_dirs, atoi is downright wrong. We are parsing UID_MIN, UID_MAX, and LU_UIDNUMBER, which all have a numerical value, without any validation that what we are parsing is actually a number. This is especially problematic as that means that in the case of an invalid value (e.g. UID_MIN=foo), UID_MIN is incorrectly parsed as 0.
Instead, use strtoul (3) to parse these values. If parsing fails, such as in the case where UID_MIN=foo, warn that parsing failed, and use the default values for each key as specified by the manual page.
--- libsemanage/src/genhomedircon.c | 41 ++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-)
libsemanage/src/genhomedircon.c +35 -6
@@ -354,24 +354,53 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MIN" ,NULL );if (path && *path) { - temp =atoi (path); - minuid = temp; - minuid_set =1 ; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + minuid = (uid_t )val; + minuid_set =1 ; + }else { +/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key UID_MIN, is its value a number?" +" Falling back to default value of `1000`." ); + minuid =1000 ; + minuid_set =1 ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MAX" ,NULL );if (path && *path) { - temp =atoi (path); - maxuid = temp; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + maxuid = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key UID_MAX, is its value a number?" +" Falling back to default value of `6000`." ); + maxuid =60000 ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LIBUSER ,"LU_UIDNUMBER" ,"=" );if (path && *path) { - temp =atoi (path); +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + temp = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key LU_UIDNUMBER, is its value a number?" +" Falling back to default value of `500`." ); + temp =500 ; + }if (!minuid_set || temp < minuid) { minuid = temp; minuid_set =1 ;
Signature · 1 line
2.50.1
Stephen Smalley
maintainerQuoted 36 lines · Rahul Sandhu
atoi (3) is... bugprone. It's virtually impossible to differentiate an invalid value (e.g. the string "foo") from a valid value such as "0" as 0 is returned on error! From the manual page:
Quoted 3 lines
except that
atoi()does not detect errors.
RETURN VALUE
The converted value or 0 on error.In the case of
get_home_dirs, atoi is downright wrong. We are parsingUID_MIN,UID_MAX, andLU_UIDNUMBER, which all have a numerical value, without any validation that what we are parsing is actually a number. This is especially problematic as that means that in the case of an invalid value (e.g. UID_MIN=foo),UID_MINis incorrectly parsed as 0.Instead, use strtoul (3) to parse these values. If parsing fails, such as in the case where UID_MIN=foo, warn that parsing failed, and use the default values for each key as specified by the manual page.
Signed-off-by Rahul Sandhu <[email protected]>--- libsemanage/src/genhomedircon.c | 41 ++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-)libsemanage/src/genhomedircon.c +1
-3@@ -354,24 +354,53 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MIN" ,NULL );if (path && *path) { - temp =atoi (path); - minuid = temp; - minuid_set =1 ; +char *endptr;
libsemanage/src/genhomedircon.c static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) · line 367
Quoted 10 lines
libsemanage/src/genhomedircon.c +10
-0+const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + minuid = (uid_t )val; + minuid_set =1 ; + }else { +/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key UID_MIN, is its value a number?" +" Falling back to default value of `1000`." ); + minuid =1000 ;
Here and below, it would be nice if we could use a #define, either a pre-existing one or one of our own, and avoid manual duplication of the value/string.
Quoted 9 lines
libsemanage/src/genhomedircon.c +2
-1+ minuid_set =1 ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MAX" ,NULL );if (path && *path) { - temp =atoi (path);
libsemanage/src/genhomedircon.c static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) · line 384
Quoted 10 lines
libsemanage/src/genhomedircon.c +9
-1- maxuid = temp; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + maxuid = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key UID_MAX, is its value a number?" +" Falling back to default value of `6000`." );
Note the inconsistency here, which would be avoided by the approach suggested above.
Quoted 9 lines
libsemanage/src/genhomedircon.c +2
-1+ maxuid =60000 ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LIBUSER ,"LU_UIDNUMBER" ,"=" );if (path && *path) { - temp =atoi (path);
libsemanage/src/genhomedircon.c static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) · line 402
Quoted 10 lines
libsemanage/src/genhomedircon.c +10
-0+char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + temp = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key LU_UIDNUMBER, is its value a number?" +" Falling back to default value of `500`." ); + temp =500 ;
Ditto.
Quoted 8 lines
libsemanage/src/genhomedircon.c +1
-1+ }if (!minuid_set || temp < minuid) { minuid = temp; minuid_set =1 ; --2.50.1
Quoted 40 lines · Stephen Smalley
Quoted 39 lines · Rahul Sandhu
atoi (3) is... bugprone. It's virtually impossible to differentiate an invalid value (e.g. the string "foo") from a valid value such as "0" as 0 is returned on error! From the manual page:
Quoted 3 lines
except that
atoi()does not detect errors.
RETURN VALUE
The converted value or 0 on error.In the case of
get_home_dirs, atoi is downright wrong. We are parsingUID_MIN,UID_MAX, andLU_UIDNUMBER, which all have a numerical value, without any validation that what we are parsing is actually a number. This is especially problematic as that means that in the case of an invalid value (e.g. UID_MIN=foo),UID_MINis incorrectly parsed as 0.Instead, use strtoul (3) to parse these values. If parsing fails, such as in the case where UID_MIN=foo, warn that parsing failed, and use the default values for each key as specified by the manual page.
Signed-off-by Rahul Sandhu <[email protected]>--- libsemanage/src/genhomedircon.c | 41 ++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 6 deletions(-)libsemanage/src/genhomedircon.c +6
-3@@ -354,24 +354,53 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MIN" ,NULL );if (path && *path) { - temp =atoi (path); - minuid = temp; - minuid_set =1 ; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + minuid = (uid_t )val; + minuid_set =1 ; + }else {
Quoted 8 lines
libsemanage/src/genhomedircon.c static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) · line 367
Quoted 5 lines
libsemanage/src/genhomedircon.c +5
-0+/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key UID_MIN, is its value a number?" +" Falling back to default value of `1000`." ); + minuid =1000 ;Here and below, it would be nice if we could use a #define, either a pre-existing one or one of our own, and avoid manual duplication of the value/string.
At top of get_home_dirs, the minuid is set to 500 default, Should the value be reset to 500 by default here?
Quoted 1 line
libsemanage/src/genhomedircon.c static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) · line 368
Quoted 1 line
libsemanage/src/genhomedircon.c +1
-0+ minuid_set =1 ;
In both the if and else branches,|minuid_set|is set to 1 — could this be moved outside the conditional blocks and implemented with just a single line of code?
Quoted 5 lines
Quoted 5 lines
libsemanage/src/genhomedircon.c +1
-0+ } }free (path); path =NULL ;
Quoted 13 lines
libsemanage/src/genhomedircon.c static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) · line 384
Quoted 13 lines
libsemanage/src/genhomedircon.c +9
-2path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MAX" ,NULL );if (path && *path) { - temp =atoi (path); - maxuid = temp; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + maxuid = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key UID_MAX, is its value a number?" +" Falling back to default value of `6000`." );
6000 or 60000?
Quoted 32 lines
Note the inconsistency here, which would be avoided by the approach suggested above.
Quoted 9 lines
libsemanage/src/genhomedircon.c +2
-1+ maxuid =60000 ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LIBUSER ,"LU_UIDNUMBER" ,"=" );if (path && *path) { - temp =atoi (path);libsemanage/src/genhomedircon.c static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) · line 402
Quoted 10 lines
libsemanage/src/genhomedircon.c +10
-0+char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + temp = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +WARN (s->h_semanage, +"Conversion failed for key LU_UIDNUMBER, is its value a number?" +" Falling back to default value of `500`." ); + temp =500 ;Ditto.
Quoted 8 lines
libsemanage/src/genhomedircon.c +1
-1+ }if (!minuid_set || temp < minuid) { minuid = temp; minuid_set =1 ; --2.50.1
PATCH v2 1/1 libsemanage: get_home_dirs: cleanup parsing of values from conf files
In reply to Stephen Smalleyatoi (3) is... bugprone. It's virtually impossible to differentiate an invalid value (e.g. the string "foo") from a valid value such as "0" as 0 is returned on error! From the manual page:
Quoted 3 lines
except that
atoi()does not detect errors.
RETURN VALUE
The converted value or 0 on error.
In the case of get_home_dirs, atoi is downright wrong. We are parsing UID_MIN, UID_MAX, and LU_UIDNUMBER, which all have a numerical value, without any validation that what we are parsing is actually a number. This is especially problematic as that means that in the case of an invalid value (e.g. UID_MIN=foo), UID_MIN is incorrectly parsed as 0.
Instead, use strtoul (3) to parse these values. If parsing fails, such as in the case where UID_MIN=foo, warn that parsing failed, and use the default values for each key as specified by the manual page.
--- libsemanage/src/genhomedircon.c | 47 ++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-)
v2: use a macro for the default values and error string
libsemanage/src/genhomedircon.c +41 -6
@@ -64,6 +64,11 @@#define PATH_SHELLS_FILE "/etc/shells"#define PATH_NOLOGIN_SHELL "/sbin/nologin" +/* fallback values */ +#define FALLBACK_MINUID 1000 +#define FALLBACK_MAXUID 60000 +#define FALLBACK_LU_UIDNUMBER 500 +/* comments written to context file */ #define COMMENT_FILE_CONTEXT_HEADER "#\n#\n# " \ "User-specific file contexts, generated via libsemanage\n" \
@@ -352,26 +357,54 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) goto fail; } +#define genhomedircon_warn_conv_fail (key, val) \ + WARN(s->h_semanage, \ + "Conversion failed for key " key ", is its value a number?" \ + " Falling back to default value of `%s`.", #val); + path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MIN" ,NULL );if (path && *path) { - temp =atoi (path); - minuid = temp; - minuid_set =1 ; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + minuid = (uid_t )val; + minuid_set =1 ; + }else { +/* we were provided an invalid value, use defaults. */ +genhomedircon_warn_conv_fail ("UID_MIN" ,FALLBACK_MINUID ); + minuid =FALLBACK_MINUID ; + minuid_set =1 ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MAX" ,NULL );if (path && *path) { - temp =atoi (path); - maxuid = temp; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + maxuid = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +genhomedircon_warn_conv_fail ("UID_MAX" ,FALLBACK_MAXUID ); + maxuid =FALLBACK_MAXUID ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LIBUSER ,"LU_UIDNUMBER" ,"=" );if (path && *path) { - temp =atoi (path); +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + temp = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +genhomedircon_warn_conv_fail ("LU_UIDNUMBER" ,FALLBACK_LU_UIDNUMBER ); + temp =FALLBACK_LU_UIDNUMBER ; + }if (!minuid_set || temp < minuid) { minuid = temp; minuid_set =1 ;
@@ -380,6 +413,8 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)free (path); path =NULL ; +#undef genhomedircon_warn_conv_fail + errno =0 ;setpwent ();while (1 ) {
Signature · 1 line
2.50.1
Quoted 15 lines · Rahul Sandhu
atoi (3) is... bugprone. It's virtually impossible to differentiate an invalid value (e.g. the string "foo") from a valid value such as "0" as 0 is returned on error! From the manual page:
Quoted 3 lines
except that
atoi()does not detect errors.
RETURN VALUE
The converted value or 0 on error.In the case of
get_home_dirs, atoi is downright wrong. We are parsingUID_MIN,UID_MAX, andLU_UIDNUMBER, which all have a numerical value, without any validation that what we are parsing is actually a number. This is especially problematic as that means that in the case of an invalid value (e.g. UID_MIN=foo),UID_MINis incorrectly parsed as 0.
Quoted 5 lines
Instead, use strtoul (3) to parse these values. If parsing fails, such as in the case where UID_MIN=foo, warn that parsing failed, and use the default values for each key as specified by the manual page.
Signed-off-by Rahul Sandhu <[email protected]>
Quoted 95 lines
--- libsemanage/src/genhomedircon.c | 47 ++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-)v2: use a macro for the default values and error string
libsemanage/src/genhomedircon.c +41
-7@@ -64,6 +64,11 @@#define PATH_SHELLS_FILE "/etc/shells"#define PATH_NOLOGIN_SHELL "/sbin/nologin" +/* fallback values */ +#define FALLBACK_MINUID 1000 +#define FALLBACK_MAXUID 60000 +#define FALLBACK_LU_UIDNUMBER 500 +/* comments written to context file */ #define COMMENT_FILE_CONTEXT_HEADER "#\n#\n# " \ "User-specific file contexts, generated via libsemanage\n" \@@ -352,26 +357,54 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s) goto fail; } +#define genhomedircon_warn_conv_fail (key, val) \ + WARN(s->h_semanage, \ + "Conversion failed for key " key ", is its value a number?" \ + " Falling back to default value of `%s`.", #val); + path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MIN" ,NULL );if (path && *path) { - temp =atoi (path); - minuid = temp; - minuid_set =1 ; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + minuid = (uid_t )val; + minuid_set =1 ; + }else { +/* we were provided an invalid value, use defaults. */ +genhomedircon_warn_conv_fail ("UID_MIN" ,FALLBACK_MINUID ); + minuid =FALLBACK_MINUID ; + minuid_set =1 ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LOGIN_DEFS ,"UID_MAX" ,NULL );if (path && *path) { - temp =atoi (path); - maxuid = temp; +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + maxuid = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +genhomedircon_warn_conv_fail ("UID_MAX" ,FALLBACK_MAXUID ); + maxuid =FALLBACK_MAXUID ; + } }free (path); path =NULL ; path =semanage_findval (PATH_ETC_LIBUSER ,"LU_UIDNUMBER" ,"=" );if (path && *path) { - temp =atoi (path); +char *endptr; +const unsigned long val =strtoul (path, &endptr,0 ); +if (endptr != path && *endptr =='\0' ) { + temp = (uid_t )val; + }else { +/* we were provided an invalid value, use defaults. */ +genhomedircon_warn_conv_fail ("LU_UIDNUMBER" ,FALLBACK_LU_UIDNUMBER ); + temp =FALLBACK_LU_UIDNUMBER ; + }if (!minuid_set || temp < minuid) { minuid = temp; minuid_set =1 ;@@ -380,6 +413,8 @@ static semanage_list_t *get_home_dirs(genhomedircon_settings_t * s)free (path); path =NULL ; +#undef genhomedircon_warn_conv_fail + errno =0 ;setpwent ();while (1 ) { --2.50.1
Stephen Smalley
maintainerQuoted 17 lines · Stephen Smalley
Quoted 15 lines · Rahul Sandhu
atoi (3) is... bugprone. It's virtually impossible to differentiate an invalid value (e.g. the string "foo") from a valid value such as "0" as 0 is returned on error! From the manual page:
Quoted 3 lines
except that
atoi()does not detect errors.
RETURN VALUE
The converted value or 0 on error.In the case of
get_home_dirs, atoi is downright wrong. We are parsingUID_MIN,UID_MAX, andLU_UIDNUMBER, which all have a numerical value, without any validation that what we are parsing is actually a number. This is especially problematic as that means that in the case of an invalid value (e.g. UID_MIN=foo),UID_MINis incorrectly parsed as 0.
Quoted 7 lines
Quoted 5 lines
Instead, use strtoul (3) to parse these values. If parsing fails, such as in the case where UID_MIN=foo, warn that parsing failed, and use the default values for each key as specified by the manual page.
Signed-off-by Rahul Sandhu <[email protected]>Acked-by Stephen Smalley <[email protected]>
Thanks, merged.