Thread (15 messages) 15 messages, 2 authors, 2022-07-27

Re: [RFC PATCH 3/6] sysctl, mod_devicetable: shadow struct ctl_table.procname for file2alias

From: Mauricio Faria de Oliveira <hidden>
Date: 2022-07-27 18:08:48
Also in: linux-fsdevel, linux-kbuild, lkml

On Tue, Jul 26, 2022 at 6:27 AM Masahiro Yamada [off-list ref] wrote:
On Fri, Jul 22, 2022 at 11:24 AM Mauricio Faria de Oliveira
[off-list ref] wrote:
quoted
In order to expose a sysctl entry to modpost (file2alias.c, precisely)
we have to shadow 'struct ctl_table' in mod_devicetable.h, as scripts
should not access kernel headers or its types (see file2alias.c).

The required field is '.procname' (basename of '/proc/sys/.../entry').

Since 'struct ctl_table' is annotated for structure randomization and
we need a known offset for '.procname' (remember, no kernel headers),
take it out of the randomized portion (as in, eg, 'struct task_struct').

Of course, add build-time checks for struct size and .procname offset
between both structs. (This has to be done on kernel side; for headers.)

With that in place, use the regular macros in devicetable-offsets.c to
define SIZE_... and OFF_... macros for the shadow struct and the field
of interest.

Signed-off-by: Mauricio Faria de Oliveira <redacted>
---
 fs/proc/proc_sysctl.c             | 19 +++++++++++++++++++
 include/linux/mod_devicetable.h   | 25 +++++++++++++++++++++++++
 include/linux/sysctl.h            | 11 ++++++++++-
 kernel/sysctl.c                   |  1 +
 scripts/mod/devicetable-offsets.c |  3 +++
 5 files changed, 58 insertions(+), 1 deletion(-)
diff --git a/fs/proc/proc_sysctl.c b/fs/proc/proc_sysctl.c
index 021e83fe831f..ebbf8702387e 100644
--- a/fs/proc/proc_sysctl.c
+++ b/fs/proc/proc_sysctl.c
@@ -19,6 +19,24 @@
 #include <linux/kmemleak.h>
 #include "internal.h"

+#ifdef CONFIG_MODULES
+#include <linux/mod_devicetable.h>
+
+static void check_struct_sysctl_device_id(void)
+{
+       /*
+        * The shadow struct sysctl_device_id for file2alias.c needs
+        * the same size of struct ctl_table and offset for procname.
+        */
+       BUILD_BUG_ON(sizeof(struct sysctl_device_id)
+                       != sizeof(struct ctl_table));
+       BUILD_BUG_ON(offsetof(struct sysctl_device_id, procname)
+                       != offsetof(struct ctl_table, procname));

Nit:

If you use static_assert(), you can remove
 check_struct_sysctl_device_id().


You can write static_assert() out of a function.
That's a nice cleanup; thanks!

quoted
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 223376959d29..15073621cfa8 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -2487,6 +2487,7 @@ int __init sysctl_init_bases(void)

        return 0;
 }
+

Noise.
Fixed.



quoted
 #endif /* CONFIG_SYSCTL */
 /*
  * No sense putting this after each symbol definition, twice,
diff --git a/scripts/mod/devicetable-offsets.c b/scripts/mod/devicetable-offsets.c
index c0d3bcb99138..43b2549940d2 100644
--- a/scripts/mod/devicetable-offsets.c
+++ b/scripts/mod/devicetable-offsets.c
@@ -262,5 +262,8 @@ int main(void)
        DEVID(ishtp_device_id);
        DEVID_FIELD(ishtp_device_id, guid);

+       DEVID(sysctl_device_id);
+       DEVID_FIELD(sysctl_device_id, procname);
+
        return 0;
 }
--
2.25.1

--
Best Regards
Masahiro Yamada


--
Mauricio Faria de Oliveira
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help