Skip to content

Commit 0fe9841

Browse files
torvaldsKali-
authored andcommitted
next_pidmap: fix overflow condition
commit c78193e9c7bcbf25b8237ad0dec82f805c4ea69b upstream. next_pidmap() just quietly accepted whatever 'last' pid that was passed in, which is not all that safe when one of the users is /proc. Admittedly the proc code should do some sanity checking on the range (and that will be the next commit), but that doesn't mean that the helper functions should just do that pidmap pointer arithmetic without checking the range of its arguments. So clamp 'last' to PID_MAX_LIMIT. The fact that we then do "last+1" doesn't really matter, the for-loop does check against the end of the pidmap array properly (it's only the actual pointer arithmetic overflow case we need to worry about, and going one bit beyond isn't going to overflow). [ Use PID_MAX_LIMIT rather than pid_max as per Eric Biederman ] Reported-by: Tavis Ormandy <[email protected]> Analyzed-by: Robert Święcki <[email protected]> Cc: Eric W. Biederman <[email protected]> Cc: Pavel Emelyanov <[email protected]> Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: Andi Kleen <[email protected]>
1 parent 1f46f51 commit 0fe9841

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

include/linux/pid.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ extern struct pid *find_vpid(int nr);
117117
*/
118118
extern struct pid *find_get_pid(int nr);
119119
extern struct pid *find_ge_pid(int nr, struct pid_namespace *);
120-
int next_pidmap(struct pid_namespace *pid_ns, int last);
120+
int next_pidmap(struct pid_namespace *pid_ns, unsigned int last);
121121

122122
extern struct pid *alloc_pid(struct pid_namespace *ns);
123123
extern void free_pid(struct pid *pid);

kernel/pid.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,14 @@ static int alloc_pidmap(struct pid_namespace *pid_ns)
183183
return -1;
184184
}
185185

186-
int next_pidmap(struct pid_namespace *pid_ns, int last)
186+
int next_pidmap(struct pid_namespace *pid_ns, unsigned int last)
187187
{
188188
int offset;
189189
struct pidmap *map, *end;
190190

191+
if (last >= PID_MAX_LIMIT)
192+
return -1;
193+
191194
offset = (last + 1) & BITS_PER_PAGE_MASK;
192195
map = &pid_ns->pidmap[(last + 1)/BITS_PER_PAGE];
193196
end = &pid_ns->pidmap[PIDMAP_ENTRIES];

0 commit comments

Comments
 (0)