Skip to contentSkip to conversation

Provide linux/set_memory.h

RFC PATCH Applied Michael Ellerman 4 participants 9 messages Jun 29, 2017 – Jun 30, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/4 Provide linux/set_memory.h

Currently code that wants to use set_memory_ro() etc, needs to include asm/set_memory.h, which doesn't exist on all arches. Some code knows it only builds on arches which have the header, other code guards the inclusion with an #ifdef, neither is ideal.

So create linux/set_memory.h. This always exists, so users don't need an #ifdef just to include the header.

When CONFIG_ARCH_HAS_SET_MEMORY=y it includes asm/set_memory.h, otherwise it provides empty non-failing implementations.

---
 include/linux/set_memory.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 include/linux/set_memory.h

Does this look OK to people? If so it would be great if someone, Kees?, Andrew?, could pick up patch 1 (it's a nop by itself) and then we can send the conversions separately later in the merge window?

cheers

include/linux/set_memory.h new file +20 -0
new file mode 100644
index 000000000000..e5140648f638
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017, Michael Ellerman, IBM Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation;
+ */
+#ifndef _LINUX_SET_MEMORY_H_
+#define _LINUX_SET_MEMORY_H_
+
+#ifdef CONFIG_ARCH_HAS_SET_MEMORY
+#include <asm/set_memory.h>
+#else
+static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; }
+static inline int set_memory_rw(unsigned long addr, int numpages) { return 0; }
+static inline int set_memory_x(unsigned long addr,  int numpages) { return 0; }
+static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; }
+#endif
+
+#endif /* _LINUX_SET_MEMORY_H_ */
Signature · 1 line
2.7.4
#1 lore raw

RFC PATCH 2/4 PM / hibernate: Use linux/set_memory.h

This header always exists, so doesn't require an ifdef around its inclusion. When CONFIG_ARCH_HAS_SET_MEMORY=y it includes the asm header, otherwise it provides empty versions of the set_memory_xx() routines.

---
 kernel/power/snapshot.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/power/snapshot.c +1 -3
index b7708e319941..222317721c5a 100644
@@ -30,15 +30,13 @@
 #include <linux/slab.h>
 #include <linux/compiler.h>
 #include <linux/ktime.h>
+#include <linux/set_memory.h>
 
 #include <linux/uaccess.h>
 #include <asm/mmu_context.h>
 #include <asm/pgtable.h>
 #include <asm/tlbflush.h>
 #include <asm/io.h>
-#ifdef CONFIG_ARCH_HAS_SET_MEMORY
-#include <asm/set_memory.h>
-#endif
 
 #include "power.h"
 
Signature · 1 line
2.7.4
#2 lore raw

RFC PATCH 3/4 module: Use linux/set_memory.h

In reply to Michael Ellerman

This header always exists, so doesn't require an ifdef around its inclusion. When CONFIG_ARCH_HAS_SET_MEMORY=y it includes the asm header, otherwise it provides empty versions of the set_memory_xx() routines.

The usages of set_memory_xx() are still guarded by CONFIG_STRICT_MODULE_RWX.

---
 kernel/module.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/module.c +1 -3
index d0a723ebe75c..e7696b25db30 100644
@@ -49,9 +49,7 @@
 #include <linux/rculist.h>
 #include <linux/uaccess.h>
 #include <asm/cacheflush.h>
-#ifdef CONFIG_STRICT_MODULE_RWX
-#include <asm/set_memory.h>
-#endif
+#include <linux/set_memory.h>
 #include <asm/mmu_context.h>
 #include <linux/license.h>
 #include <asm/sections.h>
Signature · 1 line
2.7.4
#3 lore raw

RFC PATCH 4/4 bpf: Use linux/set_memory.h

In reply to Michael Ellerman

This header always exists, so doesn't require an ifdef around its inclusion. When CONFIG_ARCH_HAS_SET_MEMORY=y it includes the asm header, otherwise it provides empty versions of the set_memory_xx() routines.

---
 include/linux/filter.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
include/linux/filter.h +1 -4
index 1fa26dc562ce..54f26e9c6472 100644
@@ -16,13 +16,10 @@
 #include <linux/sched.h>
 #include <linux/capability.h>
 #include <linux/cryptohash.h>
+#include <linux/set_memory.h>
 
 #include <net/sch_generic.h>
 
-#ifdef CONFIG_ARCH_HAS_SET_MEMORY
-#include <asm/set_memory.h>
-#endif
-
 #include <uapi/linux/filter.h>
 #include <uapi/linux/bpf.h>
 
Signature · 1 line
2.7.4
maintainer Acked-by on v1 4/4
#4 lore raw

Re: [kernel-hardening] [RFC PATCH 4/4] bpf: Use linux/set_memory.h

Quoted 5 lines · Michael Ellerman

This header always exists, so doesn't require an ifdef around its inclusion. When CONFIG_ARCH_HAS_SET_MEMORY=y it includes the asm header, otherwise it provides empty versions of the set_memory_xx() routines.

maintainer Acked-by on v1 1/4
#5 lore raw
In reply to Michael Ellerman
Quoted 5 lines · Michael Ellerman

Currently code that wants to use set_memory_ro() etc, needs to include asm/set_memory.h, which doesn't exist on all arches. Some code knows it only builds on arches which have the header, other code guards the inclusion with an #ifdef, neither is ideal.

Quoted 7 lines

So create linux/set_memory.h. This always exists, so users don't need an #ifdef just to include the header.

When CONFIG_ARCH_HAS_SET_MEMORY=y it includes asm/set_memory.h, otherwise it provides empty non-failing implementations.

Looks good to me, thanks!

I'm fine if Andrew or Kees picks up the bpf patch as well, I think there shouldn't be any conflict with net-next on this one (and even if so, then looks trivial to resolve).

Acked-by
#6 lore raw
Quoted 16 lines · Daniel Borkmann
Quoted 6 lines · Michael Ellerman

Currently code that wants to use set_memory_ro() etc, needs to include asm/set_memory.h, which doesn't exist on all arches. Some code knows it only builds on arches which have the header, other code guards the inclusion with an #ifdef, neither is ideal.

Quoted 7 lines

So create linux/set_memory.h. This always exists, so users don't need an #ifdef just to include the header.

When CONFIG_ARCH_HAS_SET_MEMORY=y it includes asm/set_memory.h, otherwise it provides empty non-failing implementations.

Quoted 7 lines

Looks good to me, thanks!

I'm fine if Andrew or Kees picks up the bpf patch as well, I think there shouldn't be any conflict with net-next on this one (and even if so, then looks trivial to resolve).

I nominate Andrew. ;) This should go in early in the merge window and the users can go late in the window. If Andrew has enough to do, I can carry it too; just say the word.

This is a sane addition and allows for lines-of-code reduction in a few places. Thanks!

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#7 lore raw
Quoted 25 lines · Kees Cook
Quoted 16 lines · Daniel Borkmann
Quoted 6 lines · Michael Ellerman

Currently code that wants to use set_memory_ro() etc, needs to include asm/set_memory.h, which doesn't exist on all arches. Some code knows it only builds on arches which have the header, other code guards the inclusion with an #ifdef, neither is ideal.

Quoted 7 lines

So create linux/set_memory.h. This always exists, so users don't need an #ifdef just to include the header.

When CONFIG_ARCH_HAS_SET_MEMORY=y it includes asm/set_memory.h, otherwise it provides empty non-failing implementations.

Quoted 7 lines

Looks good to me, thanks!

I'm fine if Andrew or Kees picks up the bpf patch as well, I think there shouldn't be any conflict with net-next on this one (and even if so, then looks trivial to resolve).

Quoted 6 lines

I nominate Andrew. ;) This should go in early in the merge window and the users can go late in the window. If Andrew has enough to do, I can carry it too; just say the word.

This is a sane addition and allows for lines-of-code reduction in a few places. Thanks!

Andrew's picked them up in mmotm, thanks everyone.

cheers

Acked-by on v1 1/4
#8 lore raw
In reply to Michael Ellerman
Quoted 18 lines · Michael Ellerman

Currently code that wants to use set_memory_ro() etc, needs to include asm/set_memory.h, which doesn't exist on all arches. Some code knows it only builds on arches which have the header, other code guards the inclusion with an #ifdef, neither is ideal.

So create linux/set_memory.h. This always exists, so users don't need an #ifdef just to include the header.

When CONFIG_ARCH_HAS_SET_MEMORY=y it includes asm/set_memory.h, otherwise it provides empty non-failing implementations.

---
 include/linux/set_memory.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
 create mode 100644 include/linux/set_memory.h
Quoted 4 lines

Does this look OK to people? If so it would be great if someone, Kees?, Andrew?, could pick up patch 1 (it's a nop by itself) and then we can send the conversions separately later in the merge window?

Quoted 29 lines

cheers

include/linux/set_memory.h new file +20 -0
new file mode 100644
index 000000000000..e5140648f638
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017, Michael Ellerman, IBM Corporation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation;
+ */
+#ifndef _LINUX_SET_MEMORY_H_
+#define _LINUX_SET_MEMORY_H_
+
+#ifdef CONFIG_ARCH_HAS_SET_MEMORY
+#include <asm/set_memory.h>
+#else
+static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; }
+static inline int set_memory_rw(unsigned long addr, int numpages) { return 0; }
+static inline int set_memory_x(unsigned long addr,  int numpages) { return 0; }
+static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; }
+#endif
+
+#endif /* _LINUX_SET_MEMORY_H_ */