* [PATCH] lkdtm: fix potential use after free
@ 2019-04-24 9:59 Weikang shi
2019-04-24 12:24 ` Mark Rutland
0 siblings, 1 reply; 4+ messages in thread
From: Weikang shi @ 2019-04-24 9:59 UTC (permalink / raw)
To: keescook; +Cc: arnd, gregkh, linux-kernel, swkhack
From: swkhack <swkhack@gmail.com>
The function lkdtm_READ_AFTER_FREE calls kfree(base) to free the memory
of base. However, following kfree(base),
it access the memory which base point to via base[offset]. This may result in a
use-after-free bug. This patch moves kfree(base) after the dereference.
Signed-off-by: swkhack <swkhack@gmail.com>
---
drivers/misc/lkdtm/heap.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/misc/lkdtm/heap.c b/drivers/misc/lkdtm/heap.c
index 65026d7de..3e2f1580a 100644
--- a/drivers/misc/lkdtm/heap.c
+++ b/drivers/misc/lkdtm/heap.c
@@ -77,7 +77,6 @@ void lkdtm_READ_AFTER_FREE(void)
base[offset] = *val;
pr_info("Value in memory before free: %x\n", base[offset]);
- kfree(base);
pr_info("Attempting bad read from freed memory\n");
saw = base[offset];
@@ -88,6 +87,7 @@ void lkdtm_READ_AFTER_FREE(void)
}
pr_info("Memory was not poisoned\n");
+ kfree(base);
kfree(val);
}
--
2.17.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] lkdtm: fix potential use after free
2019-04-24 9:59 [PATCH] lkdtm: fix potential use after free Weikang shi
@ 2019-04-24 12:24 ` Mark Rutland
0 siblings, 0 replies; 4+ messages in thread
From: Mark Rutland @ 2019-04-24 12:24 UTC (permalink / raw)
To: Weikang shi; +Cc: keescook, arnd, gregkh, linux-kernel
Hi,
On Wed, Apr 24, 2019 at 05:59:52PM +0800, Weikang shi wrote:
> From: swkhack <swkhack@gmail.com>
>
> The function lkdtm_READ_AFTER_FREE calls kfree(base) to free the memory
> of base. However, following kfree(base),
> it access the memory which base point to via base[offset]. This may result in a
> use-after-free bug.
This is deliberate; the test is designed to provoke a use-after-free to
verify that this can be detected by tools such as KASAN.
> This patch moves kfree(base) after the dereference.
Doing this would break the test, so we should not make this change.
Was this something you spotted with a static analysis tool?
> Signed-off-by: swkhack <swkhack@gmail.com>
A signed-off-by line should have your real name rather than an alias.
Thanks,
Mark.
> ---
> drivers/misc/lkdtm/heap.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/misc/lkdtm/heap.c b/drivers/misc/lkdtm/heap.c
> index 65026d7de..3e2f1580a 100644
> --- a/drivers/misc/lkdtm/heap.c
> +++ b/drivers/misc/lkdtm/heap.c
> @@ -77,7 +77,6 @@ void lkdtm_READ_AFTER_FREE(void)
> base[offset] = *val;
> pr_info("Value in memory before free: %x\n", base[offset]);
>
> - kfree(base);
>
> pr_info("Attempting bad read from freed memory\n");
> saw = base[offset];
> @@ -88,6 +87,7 @@ void lkdtm_READ_AFTER_FREE(void)
> }
> pr_info("Memory was not poisoned\n");
>
> + kfree(base);
> kfree(val);
> }
>
> --
> 2.17.1
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] lkdtm: fix potential use after free
2019-04-24 10:21 Weikang shi
@ 2019-04-24 12:24 ` Mark Rutland
0 siblings, 0 replies; 4+ messages in thread
From: Mark Rutland @ 2019-04-24 12:24 UTC (permalink / raw)
To: Weikang shi; +Cc: keescook, arnd, gregkh, linux-kernel
On Wed, Apr 24, 2019 at 06:21:03PM +0800, Weikang shi wrote:
> From: swkhack <swkhack@gmail.com>
>
> The function lkdtm_WRITE_AFTER_FREE calls kfree(base) to free the memory
> of base. However, following kfree(base),
> it write the memory which base point to via base[offset] = 0x0abcdef0. This may result in a
> use-after-free bug. This patch moves kfree(base) after the write.
As with lkdtm_READ_AFTER_FREE, this is deliberate, and we should not
make this change.
Thanks,
Mark.
>
> Signed-off-by: swkhack <swkhack@gmail.com>
> ---
> drivers/misc/lkdtm/heap.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/misc/lkdtm/heap.c b/drivers/misc/lkdtm/heap.c
> index 65026d7de..0b9141525 100644
> --- a/drivers/misc/lkdtm/heap.c
> +++ b/drivers/misc/lkdtm/heap.c
> @@ -40,8 +40,8 @@ void lkdtm_WRITE_AFTER_FREE(void)
> pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
> pr_info("Attempting bad write to freed memory at %p\n",
> &base[offset]);
> - kfree(base);
> base[offset] = 0x0abcdef0;
> + kfree(base);
> /* Attempt to notice the overwrite. */
> again = kmalloc(len, GFP_KERNEL);
> kfree(again);
> --
> 2.17.1
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH] lkdtm: fix potential use after free
@ 2019-04-24 10:21 Weikang shi
2019-04-24 12:24 ` Mark Rutland
0 siblings, 1 reply; 4+ messages in thread
From: Weikang shi @ 2019-04-24 10:21 UTC (permalink / raw)
To: keescook; +Cc: arnd, gregkh, linux-kernel, swkhack
From: swkhack <swkhack@gmail.com>
The function lkdtm_WRITE_AFTER_FREE calls kfree(base) to free the memory
of base. However, following kfree(base),
it write the memory which base point to via base[offset] = 0x0abcdef0. This may result in a
use-after-free bug. This patch moves kfree(base) after the write.
Signed-off-by: swkhack <swkhack@gmail.com>
---
drivers/misc/lkdtm/heap.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/misc/lkdtm/heap.c b/drivers/misc/lkdtm/heap.c
index 65026d7de..0b9141525 100644
--- a/drivers/misc/lkdtm/heap.c
+++ b/drivers/misc/lkdtm/heap.c
@@ -40,8 +40,8 @@ void lkdtm_WRITE_AFTER_FREE(void)
pr_info("Allocated memory %p-%p\n", base, &base[offset * 2]);
pr_info("Attempting bad write to freed memory at %p\n",
&base[offset]);
- kfree(base);
base[offset] = 0x0abcdef0;
+ kfree(base);
/* Attempt to notice the overwrite. */
again = kmalloc(len, GFP_KERNEL);
kfree(again);
--
2.17.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2019-04-24 12:24 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-24 9:59 [PATCH] lkdtm: fix potential use after free Weikang shi
2019-04-24 12:24 ` Mark Rutland
2019-04-24 10:21 Weikang shi
2019-04-24 12:24 ` Mark Rutland
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).