Skip to content

Commit ce1091d

Browse files
Villemoestorvalds
authored andcommitted
lib/bitmap.c: fix remaining space computation in bitmap_print_to_pagebuf
For various alignments of buf, the current expression computes 4096 ok 4095 ok 8190 8189 ... 4097 i.e., if the caller has already written two bytes into the page buffer, len is 8190 rather than 4094, because PTR_ALIGN aligns up to the next boundary. So if the printed version of the bitmap is huge, scnprintf() ends up writing beyond the page boundary. I don't think any current callers actually write anything before bitmap_print_to_pagebuf, but the API seems to be designed to allow it. [akpm@linux-foundation.org: use offset_in_page(), per Andy] [akpm@linux-foundation.org: include mm.h for offset_in_page()] Link: http://lkml.kernel.org/r/20180818131623.8755-7-linux@rasmusvillemoes.dk Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com> Cc: Yury Norov <ynorov@caviumnetworks.com> Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk> Cc: Sudeep Holla <sudeep.holla@arm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 41e7b16 commit ce1091d

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

‎lib/bitmap.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/bitops.h>
1414
#include <linux/bug.h>
1515
#include <linux/kernel.h>
16+
#include <linux/mm.h>
1617
#include <linux/slab.h>
1718
#include <linux/string.h>
1819
#include <linux/uaccess.h>
@@ -461,14 +462,15 @@ EXPORT_SYMBOL(bitmap_parse_user);
461462
* ranges if list is specified or hex digits grouped into comma-separated
462463
* sets of 8 digits/set. Returns the number of characters written to buf.
463464
*
464-
* It is assumed that @buf is a pointer into a PAGE_SIZE area and that
465-
* sufficient storage remains at @buf to accommodate the
466-
* bitmap_print_to_pagebuf() output.
465+
* It is assumed that @buf is a pointer into a PAGE_SIZE, page-aligned
466+
* area and that sufficient storage remains at @buf to accommodate the
467+
* bitmap_print_to_pagebuf() output. Returns the number of characters
468+
* actually printed to @buf, excluding terminating '\0'.
467469
*/
468470
int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp,
469471
int nmaskbits)
470472
{
471-
ptrdiff_t len = PTR_ALIGN(buf + PAGE_SIZE - 1, PAGE_SIZE) - buf;
473+
ptrdiff_t len = PAGE_SIZE - offset_in_page(buf);
472474
int n = 0;
473475

474476
if (len > 1)

0 commit comments

Comments
 (0)