selinux: do not leave dangling pointer behind
authorChristian Göttsche <cgzones@googlemail.com>
Thu, 20 Apr 2023 15:04:58 +0000 (17:04 +0200)
committerPaul Moore <paul@paul-moore.com>
Mon, 8 May 2023 20:37:42 +0000 (16:37 -0400)
In case mls_context_cpy() fails due to OOM set the free'd pointer in
context_cpy() to NULL to avoid it potentially being dereferenced or
free'd again in future.  Freeing a NULL pointer is well-defined and a
hard NULL dereference crash is at least not exploitable and should give
a workable stack trace.

Fixes: 12b29f34558b ("selinux: support deferred mapping of contexts")
Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Signed-off-by: Paul Moore <paul@paul-moore.com>
security/selinux/ss/context.h

index eda32c3d4c0a85c40cd98f09e5db62305fd1398d..44179977f434ccd8ac8016ca7cbf834d86b5286d 100644 (file)
@@ -167,6 +167,7 @@ static inline int context_cpy(struct context *dst, const struct context *src)
        rc = mls_context_cpy(dst, src);
        if (rc) {
                kfree(dst->str);
+               dst->str = NULL;
                return rc;
        }
        return 0;