All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] swapfile: fix memory corruption via malformed swapfile
@ 2016-10-31 21:32 Jann Horn
  2016-10-31 22:36 ` Kees Cook
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Jann Horn @ 2016-10-31 21:32 UTC (permalink / raw)
  To: Andrew Morton, Kirill A. Shutemov, Vlastimil Babka,
	Johannes Weiner, Jerome Marchand
  Cc: linux-mm, linux-kernel

When root activates a swap partition whose header has the wrong endianness,
nr_badpages elements of badpages are swabbed before nr_badpages has been
checked, leading to a buffer overrun of up to 8GB.

This normally is not a security issue because it can only be exploited by
root (more specifically, a process with CAP_SYS_ADMIN or the ability to
modify a swap file/partition), and such a process can already e.g. modify
swapped-out memory of any other userspace process on the system.

Testcase for reproducing the bug (must be run as root, should crash your
kernel):
=================
#include <stdlib.h>
#include <unistd.h>
#include <sys/swap.h>
#include <limits.h>
#include <err.h>
#include <string.h>
#include <stdio.h>

#define PAGE_SIZE 4096
#define __u32 unsigned int


// from include/linux/swap.h
union swap_header {
  struct {
    char reserved[PAGE_SIZE - 10];
    char magic[10];     /* SWAP-SPACE or SWAPSPACE2 */
  } magic;
  struct {
    char    bootbits[1024]; /* Space for disklabel etc. */
    __u32   version;
    __u32   last_page;
    __u32   nr_badpages;
    unsigned char sws_uuid[16];
    unsigned char sws_volume[16];
    __u32   padding[117];
    __u32   badpages[1];
  } info;
};

int main(void) {
  char file[] = "/tmp/swapfile.XXXXXX";
  int file_fd = mkstemp(file);
  if (file_fd == -1)
    err(1, "mkstemp");
  if (ftruncate(file_fd, PAGE_SIZE))
    err(1, "ftruncate");
  union swap_header swap_header = {
    .info = {
      .version = __builtin_bswap32(1),
      .nr_badpages = __builtin_bswap32(INT_MAX)
    }
  };
  memcpy(swap_header.magic.magic, "SWAPSPACE2", 10);
  if (write(file_fd, &swap_header, sizeof(swap_header)) !=
      sizeof(swap_header))
    err(1, "write");

  // not because the attack needs it, just in case you forgot to
  // sync yourself before crashing your machine
  sync();

  // now die
  if (swapon(file, 0))
    err(1, "swapon");
  puts("huh, we survived");
  if (swapoff(file))
    err(1, "swapoff");
  unlink(file);
}
=================

Cc: stable@vger.kernel.org
Signed-off-by: Jann Horn <jann@thejh.net>
---
 mm/swapfile.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 2210de290b54..f30438970cd1 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2224,6 +2224,8 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
 		swab32s(&swap_header->info.version);
 		swab32s(&swap_header->info.last_page);
 		swab32s(&swap_header->info.nr_badpages);
+		if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
+			return 0;
 		for (i = 0; i < swap_header->info.nr_badpages; i++)
 			swab32s(&swap_header->info.badpages[i]);
 	}
-- 
2.1.4

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH] swapfile: fix memory corruption via malformed swapfile
@ 2016-10-31 21:32 Jann Horn
  2016-10-31 22:36 ` Kees Cook
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Jann Horn @ 2016-10-31 21:32 UTC (permalink / raw)
  To: Andrew Morton, Kirill A. Shutemov, Vlastimil Babka,
	Johannes Weiner, Jerome Marchand
  Cc: linux-mm, linux-kernel

When root activates a swap partition whose header has the wrong endianness,
nr_badpages elements of badpages are swabbed before nr_badpages has been
checked, leading to a buffer overrun of up to 8GB.

This normally is not a security issue because it can only be exploited by
root (more specifically, a process with CAP_SYS_ADMIN or the ability to
modify a swap file/partition), and such a process can already e.g. modify
swapped-out memory of any other userspace process on the system.

Testcase for reproducing the bug (must be run as root, should crash your
kernel):
=================
#include <stdlib.h>
#include <unistd.h>
#include <sys/swap.h>
#include <limits.h>
#include <err.h>
#include <string.h>
#include <stdio.h>

#define PAGE_SIZE 4096
#define __u32 unsigned int


// from include/linux/swap.h
union swap_header {
  struct {
    char reserved[PAGE_SIZE - 10];
    char magic[10];     /* SWAP-SPACE or SWAPSPACE2 */
  } magic;
  struct {
    char    bootbits[1024]; /* Space for disklabel etc. */
    __u32   version;
    __u32   last_page;
    __u32   nr_badpages;
    unsigned char sws_uuid[16];
    unsigned char sws_volume[16];
    __u32   padding[117];
    __u32   badpages[1];
  } info;
};

int main(void) {
  char file[] = "/tmp/swapfile.XXXXXX";
  int file_fd = mkstemp(file);
  if (file_fd == -1)
    err(1, "mkstemp");
  if (ftruncate(file_fd, PAGE_SIZE))
    err(1, "ftruncate");
  union swap_header swap_header = {
    .info = {
      .version = __builtin_bswap32(1),
      .nr_badpages = __builtin_bswap32(INT_MAX)
    }
  };
  memcpy(swap_header.magic.magic, "SWAPSPACE2", 10);
  if (write(file_fd, &swap_header, sizeof(swap_header)) !=
      sizeof(swap_header))
    err(1, "write");

  // not because the attack needs it, just in case you forgot to
  // sync yourself before crashing your machine
  sync();

  // now die
  if (swapon(file, 0))
    err(1, "swapon");
  puts("huh, we survived");
  if (swapoff(file))
    err(1, "swapoff");
  unlink(file);
}
=================

Cc: stable@vger.kernel.org
Signed-off-by: Jann Horn <jann@thejh.net>
---
 mm/swapfile.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 2210de290b54..f30438970cd1 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -2224,6 +2224,8 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
 		swab32s(&swap_header->info.version);
 		swab32s(&swap_header->info.last_page);
 		swab32s(&swap_header->info.nr_badpages);
+		if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
+			return 0;
 		for (i = 0; i < swap_header->info.nr_badpages; i++)
 			swab32s(&swap_header->info.badpages[i]);
 	}
-- 
2.1.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] swapfile: fix memory corruption via malformed swapfile
  2016-10-31 21:32 [PATCH] swapfile: fix memory corruption via malformed swapfile Jann Horn
@ 2016-10-31 22:36 ` Kees Cook
  2016-11-01  9:10 ` Jerome Marchand
  2016-11-04 14:57 ` Johannes Weiner
  2 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2016-10-31 22:36 UTC (permalink / raw)
  To: Jann Horn
  Cc: Andrew Morton, Kirill A. Shutemov, Vlastimil Babka,
	Johannes Weiner, Jerome Marchand, Linux-MM, LKML

On Mon, Oct 31, 2016 at 2:32 PM, Jann Horn <jann@thejh.net> wrote:
> When root activates a swap partition whose header has the wrong endianness,
> nr_badpages elements of badpages are swabbed before nr_badpages has been
> checked, leading to a buffer overrun of up to 8GB.
>
> This normally is not a security issue because it can only be exploited by
> root (more specifically, a process with CAP_SYS_ADMIN or the ability to
> modify a swap file/partition), and such a process can already e.g. modify
> swapped-out memory of any other userspace process on the system.
>
> Testcase for reproducing the bug (must be run as root, should crash your
> kernel):
> =================
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/swap.h>
> #include <limits.h>
> #include <err.h>
> #include <string.h>
> #include <stdio.h>
>
> #define PAGE_SIZE 4096
> #define __u32 unsigned int
>
>
> // from include/linux/swap.h
> union swap_header {
>   struct {
>     char reserved[PAGE_SIZE - 10];
>     char magic[10];     /* SWAP-SPACE or SWAPSPACE2 */
>   } magic;
>   struct {
>     char    bootbits[1024]; /* Space for disklabel etc. */
>     __u32   version;
>     __u32   last_page;
>     __u32   nr_badpages;
>     unsigned char sws_uuid[16];
>     unsigned char sws_volume[16];
>     __u32   padding[117];
>     __u32   badpages[1];
>   } info;
> };
>
> int main(void) {
>   char file[] = "/tmp/swapfile.XXXXXX";
>   int file_fd = mkstemp(file);
>   if (file_fd == -1)
>     err(1, "mkstemp");
>   if (ftruncate(file_fd, PAGE_SIZE))
>     err(1, "ftruncate");
>   union swap_header swap_header = {
>     .info = {
>       .version = __builtin_bswap32(1),
>       .nr_badpages = __builtin_bswap32(INT_MAX)
>     }
>   };
>   memcpy(swap_header.magic.magic, "SWAPSPACE2", 10);
>   if (write(file_fd, &swap_header, sizeof(swap_header)) !=
>       sizeof(swap_header))
>     err(1, "write");
>
>   // not because the attack needs it, just in case you forgot to
>   // sync yourself before crashing your machine
>   sync();
>
>   // now die
>   if (swapon(file, 0))
>     err(1, "swapon");
>   puts("huh, we survived");
>   if (swapoff(file))
>     err(1, "swapoff");
>   unlink(file);
> }
> =================
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Jann Horn <jann@thejh.net>
> ---
>  mm/swapfile.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 2210de290b54..f30438970cd1 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -2224,6 +2224,8 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
>                 swab32s(&swap_header->info.version);
>                 swab32s(&swap_header->info.last_page);
>                 swab32s(&swap_header->info.nr_badpages);
> +               if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
> +                       return 0;
>                 for (i = 0; i < swap_header->info.nr_badpages; i++)
>                         swab32s(&swap_header->info.badpages[i]);
>         }
> --
> 2.1.4
>

Eww. Nice find. :) At least it's only init_ns CAP_SYS_ADMIN. :P

Acked-by: Kees Cook <keescook@chromium.org>

-Kees

-- 
Kees Cook
Nexus Security

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] swapfile: fix memory corruption via malformed swapfile
@ 2016-10-31 22:36 ` Kees Cook
  0 siblings, 0 replies; 7+ messages in thread
From: Kees Cook @ 2016-10-31 22:36 UTC (permalink / raw)
  To: Jann Horn
  Cc: Andrew Morton, Kirill A. Shutemov, Vlastimil Babka,
	Johannes Weiner, Jerome Marchand, Linux-MM, LKML

On Mon, Oct 31, 2016 at 2:32 PM, Jann Horn <jann@thejh.net> wrote:
> When root activates a swap partition whose header has the wrong endianness,
> nr_badpages elements of badpages are swabbed before nr_badpages has been
> checked, leading to a buffer overrun of up to 8GB.
>
> This normally is not a security issue because it can only be exploited by
> root (more specifically, a process with CAP_SYS_ADMIN or the ability to
> modify a swap file/partition), and such a process can already e.g. modify
> swapped-out memory of any other userspace process on the system.
>
> Testcase for reproducing the bug (must be run as root, should crash your
> kernel):
> =================
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/swap.h>
> #include <limits.h>
> #include <err.h>
> #include <string.h>
> #include <stdio.h>
>
> #define PAGE_SIZE 4096
> #define __u32 unsigned int
>
>
> // from include/linux/swap.h
> union swap_header {
>   struct {
>     char reserved[PAGE_SIZE - 10];
>     char magic[10];     /* SWAP-SPACE or SWAPSPACE2 */
>   } magic;
>   struct {
>     char    bootbits[1024]; /* Space for disklabel etc. */
>     __u32   version;
>     __u32   last_page;
>     __u32   nr_badpages;
>     unsigned char sws_uuid[16];
>     unsigned char sws_volume[16];
>     __u32   padding[117];
>     __u32   badpages[1];
>   } info;
> };
>
> int main(void) {
>   char file[] = "/tmp/swapfile.XXXXXX";
>   int file_fd = mkstemp(file);
>   if (file_fd == -1)
>     err(1, "mkstemp");
>   if (ftruncate(file_fd, PAGE_SIZE))
>     err(1, "ftruncate");
>   union swap_header swap_header = {
>     .info = {
>       .version = __builtin_bswap32(1),
>       .nr_badpages = __builtin_bswap32(INT_MAX)
>     }
>   };
>   memcpy(swap_header.magic.magic, "SWAPSPACE2", 10);
>   if (write(file_fd, &swap_header, sizeof(swap_header)) !=
>       sizeof(swap_header))
>     err(1, "write");
>
>   // not because the attack needs it, just in case you forgot to
>   // sync yourself before crashing your machine
>   sync();
>
>   // now die
>   if (swapon(file, 0))
>     err(1, "swapon");
>   puts("huh, we survived");
>   if (swapoff(file))
>     err(1, "swapoff");
>   unlink(file);
> }
> =================
>
> Cc: stable@vger.kernel.org
> Signed-off-by: Jann Horn <jann@thejh.net>
> ---
>  mm/swapfile.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 2210de290b54..f30438970cd1 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -2224,6 +2224,8 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
>                 swab32s(&swap_header->info.version);
>                 swab32s(&swap_header->info.last_page);
>                 swab32s(&swap_header->info.nr_badpages);
> +               if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
> +                       return 0;
>                 for (i = 0; i < swap_header->info.nr_badpages; i++)
>                         swab32s(&swap_header->info.badpages[i]);
>         }
> --
> 2.1.4
>

Eww. Nice find. :) At least it's only init_ns CAP_SYS_ADMIN. :P

Acked-by: Kees Cook <keescook@chromium.org>

-Kees

-- 
Kees Cook
Nexus Security

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] swapfile: fix memory corruption via malformed swapfile
  2016-10-31 21:32 [PATCH] swapfile: fix memory corruption via malformed swapfile Jann Horn
  2016-10-31 22:36 ` Kees Cook
@ 2016-11-01  9:10 ` Jerome Marchand
  2016-11-04 14:57 ` Johannes Weiner
  2 siblings, 0 replies; 7+ messages in thread
From: Jerome Marchand @ 2016-11-01  9:10 UTC (permalink / raw)
  To: Jann Horn, Andrew Morton, Kirill A. Shutemov, Vlastimil Babka,
	Johannes Weiner
  Cc: linux-mm, linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 3042 bytes --]

On 10/31/2016 10:32 PM, Jann Horn wrote:
> When root activates a swap partition whose header has the wrong endianness,
> nr_badpages elements of badpages are swabbed before nr_badpages has been
> checked, leading to a buffer overrun of up to 8GB.
> 
> This normally is not a security issue because it can only be exploited by
> root (more specifically, a process with CAP_SYS_ADMIN or the ability to
> modify a swap file/partition), and such a process can already e.g. modify
> swapped-out memory of any other userspace process on the system.
> 
> Testcase for reproducing the bug (must be run as root, should crash your
> kernel):
> =================
> #include <stdlib.h>
> #include <unistd.h>
> #include <sys/swap.h>
> #include <limits.h>
> #include <err.h>
> #include <string.h>
> #include <stdio.h>
> 
> #define PAGE_SIZE 4096
> #define __u32 unsigned int
> 
> 
> // from include/linux/swap.h
> union swap_header {
>   struct {
>     char reserved[PAGE_SIZE - 10];
>     char magic[10];     /* SWAP-SPACE or SWAPSPACE2 */
>   } magic;
>   struct {
>     char    bootbits[1024]; /* Space for disklabel etc. */
>     __u32   version;
>     __u32   last_page;
>     __u32   nr_badpages;
>     unsigned char sws_uuid[16];
>     unsigned char sws_volume[16];
>     __u32   padding[117];
>     __u32   badpages[1];
>   } info;
> };
> 
> int main(void) {
>   char file[] = "/tmp/swapfile.XXXXXX";
>   int file_fd = mkstemp(file);
>   if (file_fd == -1)
>     err(1, "mkstemp");
>   if (ftruncate(file_fd, PAGE_SIZE))
>     err(1, "ftruncate");
>   union swap_header swap_header = {
>     .info = {
>       .version = __builtin_bswap32(1),
>       .nr_badpages = __builtin_bswap32(INT_MAX)
>     }
>   };
>   memcpy(swap_header.magic.magic, "SWAPSPACE2", 10);
>   if (write(file_fd, &swap_header, sizeof(swap_header)) !=
>       sizeof(swap_header))
>     err(1, "write");
> 
>   // not because the attack needs it, just in case you forgot to
>   // sync yourself before crashing your machine
>   sync();
> 
>   // now die
>   if (swapon(file, 0))
>     err(1, "swapon");
>   puts("huh, we survived");
>   if (swapoff(file))
>     err(1, "swapoff");
>   unlink(file);
> }
> =================
> 
> Cc: stable@vger.kernel.org
> Signed-off-by: Jann Horn <jann@thejh.net>
> ---
>  mm/swapfile.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index 2210de290b54..f30438970cd1 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -2224,6 +2224,8 @@ static unsigned long read_swap_header(struct swap_info_struct *p,
>  		swab32s(&swap_header->info.version);
>  		swab32s(&swap_header->info.last_page);
>  		swab32s(&swap_header->info.nr_badpages);
> +		if (swap_header->info.nr_badpages > MAX_SWAP_BADPAGES)
> +			return 0;
>  		for (i = 0; i < swap_header->info.nr_badpages; i++)
>  			swab32s(&swap_header->info.badpages[i]);
>  	}
> 

Nice catch!

Acked-by: Jerome Marchand <jmarchan@redhat.com>




[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] swapfile: fix memory corruption via malformed swapfile
  2016-10-31 21:32 [PATCH] swapfile: fix memory corruption via malformed swapfile Jann Horn
  2016-10-31 22:36 ` Kees Cook
  2016-11-01  9:10 ` Jerome Marchand
@ 2016-11-04 14:57 ` Johannes Weiner
  2 siblings, 0 replies; 7+ messages in thread
From: Johannes Weiner @ 2016-11-04 14:57 UTC (permalink / raw)
  To: Jann Horn
  Cc: Andrew Morton, Kirill A. Shutemov, Vlastimil Babka,
	Jerome Marchand, linux-mm, linux-kernel

On Mon, Oct 31, 2016 at 10:32:13PM +0100, Jann Horn wrote:
> When root activates a swap partition whose header has the wrong endianness,
> nr_badpages elements of badpages are swabbed before nr_badpages has been
> checked, leading to a buffer overrun of up to 8GB.
> 
> This normally is not a security issue because it can only be exploited by
> root (more specifically, a process with CAP_SYS_ADMIN or the ability to
> modify a swap file/partition), and such a process can already e.g. modify
> swapped-out memory of any other userspace process on the system.
> 
> Testcase for reproducing the bug (must be run as root, should crash your
> kernel):
[...]
> Cc: stable@vger.kernel.org
> Signed-off-by: Jann Horn <jann@thejh.net>

Acked-by: Johannes Weiner <hannes@cmpxchg.org>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] swapfile: fix memory corruption via malformed swapfile
@ 2016-11-04 14:57 ` Johannes Weiner
  0 siblings, 0 replies; 7+ messages in thread
From: Johannes Weiner @ 2016-11-04 14:57 UTC (permalink / raw)
  To: Jann Horn
  Cc: Andrew Morton, Kirill A. Shutemov, Vlastimil Babka,
	Jerome Marchand, linux-mm, linux-kernel

On Mon, Oct 31, 2016 at 10:32:13PM +0100, Jann Horn wrote:
> When root activates a swap partition whose header has the wrong endianness,
> nr_badpages elements of badpages are swabbed before nr_badpages has been
> checked, leading to a buffer overrun of up to 8GB.
> 
> This normally is not a security issue because it can only be exploited by
> root (more specifically, a process with CAP_SYS_ADMIN or the ability to
> modify a swap file/partition), and such a process can already e.g. modify
> swapped-out memory of any other userspace process on the system.
> 
> Testcase for reproducing the bug (must be run as root, should crash your
> kernel):
[...]
> Cc: stable@vger.kernel.org
> Signed-off-by: Jann Horn <jann@thejh.net>

Acked-by: Johannes Weiner <hannes@cmpxchg.org>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-11-04 14:57 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-31 21:32 [PATCH] swapfile: fix memory corruption via malformed swapfile Jann Horn
2016-10-31 22:36 ` Kees Cook
2016-11-01  9:10 ` Jerome Marchand
2016-11-04 14:57 ` Johannes Weiner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.