fix uid/gid > 2^31
This MR revisits a partial fix from 2018. The problem stems from incorrect handling of unsigned 32-bit uid_ts and gid_ts as signed when values are large - i.e. when the high bit is set. In that case, pgrep and pkill fail to identify processes by uid. (They succeed when finding the same processes by username.) The primary fix for this is to impliment the "FIXME" comment in proc/readproc.h, the implementation of which allows the removal of the (int) casts from the partial fix from 2018. The other fixed code in this MR consists of tests in strict_atol() that detects and errors out on overflows. References: Merge !146
This commit is contained in:
10
pgrep.c
10
pgrep.c
@ -247,8 +247,12 @@ static int strict_atol (const char *restrict str, long *restrict value)
|
||||
|
||||
for ( ; *str; ++str) {
|
||||
if (! isdigit (*str))
|
||||
return (0);
|
||||
return 0;
|
||||
if (res >= LONG_MAX / 10)
|
||||
return 0;
|
||||
res *= 10;
|
||||
if (res >= LONG_MAX - (*str - '0'))
|
||||
return 0;
|
||||
res += *str - '0';
|
||||
}
|
||||
*value = sign * res;
|
||||
@ -323,7 +327,7 @@ static int conv_uid (const char *restrict name, struct el *restrict e)
|
||||
xwarnx(_("invalid user name: %s"), name);
|
||||
return 0;
|
||||
}
|
||||
e->num = (int) pwd->pw_uid;
|
||||
e->num = pwd->pw_uid;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -340,7 +344,7 @@ static int conv_gid (const char *restrict name, struct el *restrict e)
|
||||
xwarnx(_("invalid group name: %s"), name);
|
||||
return 0;
|
||||
}
|
||||
e->num = (int) grp->gr_gid;
|
||||
e->num = grp->gr_gid;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user