slab: Redefine ZERO_SIZE_PTR to include ERR_PTR range
Subsystems (2)
PATCH 1/1 slab: Redefine ZERO_SIZE_PTR to include ERR_PTR range
Currently kfree does not accept ERR_PTR range so redefine ZERO_SIZE_PTR to include this and also change ZERO_OR_NULL_PTR macro to check this new range. With this change kfree will skip and behave as no-ops when ERR_PTR is passed.
This will help error related to ERR_PTR stand out better.
After this, we don't need to reset any ERR_PTR variable to NULL before being passed to any kfree or related wrappers calls, as everything would be handled by ZERO_SIZE_PTR itself.
This patch is verbatim from Brad Spengler/PaX Team's code in the last public patch of grsecurity/PaX based on my understanding of the code. Changes or omissions from the original code are mine and don't reflect the original grsecurity/PaX code.
--- include/linux/slab.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
include/linux/slab.h +9 -4
@@ -127,11 +127,16 @@* * ZERO_SIZE_PTR can be passed to kfree though in the same way that NULL can. * Both make kfree a no-op. +* Note: ZERO_SIZE_PTR also cover ERR_PTR Range. */ -#define ZERO_SIZE_PTR ((void *)16) - -#define ZERO_OR_NULL_PTR (x) ((unsigned long)(x) <= \ - (unsigned long)ZERO_SIZE_PTR) +#define ZERO_SIZE_PTR \ +({ \ + BUILD_BUG_ON(!(MAX_ERRNO & ~PAGE_MASK));\ + (void *)(-MAX_ERRNO-1L); \ +}) + +#define ZERO_OR_NULL_PTR (x) ((unsigned long)(x) - 1 >= \ + (unsigned long)ZERO_SIZE_PTR - 1)#include <linux/kasan.h>
Signature · 1 line
2.20.1
Quoted 1 line · Shyam Saini
This will help error related to
ERR_PTRstand out better.
Maybe make ZERO_SIZE_PTR an ERRNO value instead? Then allow ERR_PTRs to be used instead of ZERO_SIZE_PTRs
ERRNO_ZERO_OBJECT
or something like that?
Quoted 2 lines
*/
-#defineZERO_SIZE_PTR((void *)16)
#define ZERO_SIZE_PTR ((void *)-ERRNO_ZERO_OBJECT)
include/linux/slab.h line 139
Quoted 3 lines
include/linux/slab.h +3
-0+ +#define ZERO_OR_NULL_PTR (x) ((unsigned long)(x) - 1 >= \ + (unsigned long)ZERO_SIZE_PTR - 1)
And call this something different?
Quoted 10 lines · Christopher Lameter
Quoted 1 line · Shyam Saini
This will help error related to
ERR_PTRstand out better.Maybe make
ZERO_SIZE_PTRan ERRNO value instead? Then allowERR_PTRsto be used instead ofZERO_SIZE_PTRs
ERRNO_ZERO_OBJECTor something like that?
I was wondering about something like that too, but allocating zero bytes isn't actually an error, and if we have code that does something like:
void *p = my_funky_alloc(size, ...); if (IS_ERR(p)) return PTR_ERR(p);
then we might get this errno returned to userspace.
The change is definitely worth thinking about.
Quoted 2 lines · Matthew Wilcox
I was wondering about something like that too, but allocating zero bytes isn't actually an error, and if we have code that does something like:
True. But it is in a greyzone. You cannot store anything in zero bytes after all.
Hi Matthew, Christopher,
Quoted 12 lines
Quoted 8 lines
Quoted 1 line
This will help error related to
ERR_PTRstand out better.Maybe make
ZERO_SIZE_PTRan ERRNO value instead? Then allowERR_PTRsto be used instead ofZERO_SIZE_PTRs
ERRNO_ZERO_OBJECTor something like that?
I was wondering about something like that too, but allocating zero bytes isn't actually an error, and if we have code that does something like:
Quoted 8 lines
void *p = my_funky_alloc(size, ...); if (IS_ERR(p)) return PTR_ERR(p);then we might get this errno returned to userspace.
The change is definitely worth thinking about.
Any further comments on this ?
Please let me know.
Thanks!!
Quoted 6 lines · Shyam Saini
Currently kfree does not accept
ERR_PTRrange so redefineZERO_SIZE_PTRto include this and also changeZERO_OR_NULL_PTRmacro to check this new range. With this change kfree will skip and behave as no-ops whenERR_PTRis passed.This will help error related to
ERR_PTRstand out better.
What do you mean by "stand out better"? To me it sounds like before, the kernel would probably blow up in some way if you passed an error pointer into kfree(), and with this change, it will silently ignore it instead, right? If you actually wanted this kind of error to stand out, wouldn't it make more sense to add something like "if (WARN_ON(IS_ERR(x))) return;" to the implementations of kfree()? I would prefer that, since "kfree(<error pointer>)" probably indicates that someone messed up their error handling jumps.
Quoted 3 lines
After this, we don't need to reset any
ERR_PTRvariable to NULL before being passed to any kfree or related wrappers calls, as everything would be handled byZERO_SIZE_PTRitself.
With the caveat that you still can't do it in code that might be stable-backported, otherwise it will blow up occasionally in the rare case where the error path is hit?
[...]
include/linux/slab.h line 139
Quoted 8 lines
include/linux/slab.h +8
-0+#define ZERO_SIZE_PTR \ +({ \ + BUILD_BUG_ON(!(MAX_ERRNO & ~PAGE_MASK));\ + (void *)(-MAX_ERRNO-1L); \ +}) + +#define ZERO_OR_NULL_PTR (x) ((unsigned long)(x) - 1 >= \ + (unsigned long)ZERO_SIZE_PTR - 1)
If you do go through with this change, you'll probably want to adjust the message in check_bogus_address() - "null address" really isn't an appropriate error message for an address near the end of the address space.