Skip to content

RFC sysctl: add module aliases

Mauricio Faria de Oliveira bpf bridge fsverity keyrings linux-fsdevel linux-kbuild linux-rdma linux-riscv linux-s390 linux-sctp linux-wpan lkml lvs-devel mptcp netdev netfilter-devel virtualization 2 versions In review

Get series with b4
b4 am [email protected] change-id 20260818-sysctl-module-aliases-2f5801b1eb71

Versions newest first

  1. v3 RFC Latest13 patches 14 repliesIn review
  2. v2 RFC13 patches 7 repliesSuperseded

Changes in v3 from the cover letter

- All issues were reported by [email protected].
- Tested allmodconfig with option enabled on x86_64.
- Patch 2:
- Fix typo ('s/options/option/').
- Patch 3:
- Fix usage of __UNIQUE_ID().
- Add __must_be_array() in MODULE_SYSCTL_TABLE().
- Patch 4:
- Remove trailing backslash in register_sysctl().
- Patch 6:
- Fix missing __register_net_sysctl_sz() in !CONFIG_SYSCTL.
- Patch 13:
- Fix potential out-of-bounds read in do_sysctl_entry().
- Add check for entry size of zero.
- Link to v2: https://lore.kernel.org/r/[email protected]

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/13keys, pidns, fs/verity, riscv/vector: reorder '#include <linux/sysctl.h>'arch/riscv/kernel/vector.c fs/verity/init.c include/linux/key.h +1 more+3 -10 replies
2/13proc: add config option SYSCTL_MODULE_ALIASESfs/proc/Kconfig+13 -00 replies
3/13sysctl, mod_devicetable: add macro MODULE_SYSCTL_TABLEinclude/linux/mod_devicetable.h include/linux/sysctl.h scripts/mod/devicetable-offsets.c+55 -06 replies
4/13sysctl: add register_sysctl() wrapper for MODULE_SYSCTL_TABLEdrivers/parport/procfs.c include/linux/sysctl.h+54 -20 replies
5/13sysctl, parport: update register_sysctl() callers with template argumentsdrivers/parport/procfs.c+19 -70 replies
6/13sysctl, net: add register_net_sysctl{_sz}() wrappers for MODULE_SYSCTL_TABLEinclude/linux/sysctl.h include/net/net_namespace.h net/sysctl_net.c+49 -110 replies
7/13sysctl, net: update register_net_sysctl{_sz}() callers with template argumentsnet/core/neighbour.c net/core/sysctl_net_core.c net/ieee802154/6lowpan/reassembly.c +11 more+42 -200 replies
8/13sysctl, net: update register_net_sysctl_sz(ARRAY_SIZE(table_tmpl)) with template argumentsdrivers/net/vrf.c net/bridge/br_netfilter_hooks.c net/ipv4/devinet.c +11 more+18 -300 replies
9/13sysctl, ipv6: update register_net_sysctl{_sz}() callers with template argumentsinclude/net/ipv6.h net/ipv6/icmp.c net/ipv6/route.c +1 more+17 -150 replies
10/13sysctl, net: update register_net_sysctl_sz() edge caseinclude/linux/sysctl.h net/netfilter/nf_log.c+5 -70 replies
11/13sysctl: unrandomize struct ctl_table.procnameinclude/linux/sysctl.h+14 -00 replies
12/13modpost: move addend_*_rel() calls into addend_rel()scripts/mod/modpost.c scripts/mod/modpost.h+21 -160 replies
13/13modpost: handle MODULE_SYSCTL_TABLE symbolsscripts/mod/file2alias.c scripts/mod/modpost.c scripts/mod/modpost.h+269 -20 replies

Reviews trailers per patch

None of the 13 patches has a Reviewed-by or Acked-by.

PatchMauricio Faria de Oliveira
1/13
2/13
3/13
4/13
5/13
6/13
7/13
8/13
9/13
10/13
11/13Originally-by
12/13
13/13Originally-by

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH RFC v3 00/13] sysctl: add module aliases

This series adds 'sysctl:' aliases to modules that register sysctl tables; e.g.:

        $ modinfo ./mpls_router.ko  | grep sysctl:
        alias:          sysctl:*/net/mpls/conf/*/input
        alias:          sysctl:*/net/mpls/default_ttl
        alias:          sysctl:*/net/mpls/ip_ttl_propagate
        alias:          sysctl:*/net/mpls/platform_labels

It provides a trivial way to map /proc/sys files to modules (not trivial today), and for userspace to handle nonexistent /proc/sys files (e.g., procps's sysctl and systemd-sysctl applying tunables) with "modprobe sysctl:<...>" and a retry.

This is done almost automatically with register_sysctl(), register_net_sysctl() and friends as wrappers of MODULE_SYSCTL_TABLE (similar to MODULE_DEVICE_TABLE), which emits symbols for file2alias/modpost to find and parse the sysctl tables.

The big exception to 'almost' are sysctl tables and paths allocated or defined at runtime (e.g., per-namespace or per-device), as all information is required at build-time. Fortunately, such tables and paths are often based on 'templates' which are static and can be used.

This is done by plumbing the template table/path as optional arguments (macros with default values as default_gfp()), so not to create functions for all cases:

        register_sysctl(path, table [, table_tmpl [, path_tmpl]]);
        register_net_sysctl(net, path, table [, table_tmpl[, path_tmpl]]);
        register_net_sysctl_sz(net, path, table, size [, table_tmpl[, path_tmpl]]);

In this series:
- Patch 1 prevents a build error later.
- Patch 2 adds CONFIG_SYSCTL_MODULE_ALIASES.
- Patch 3 adds MODULE_SYSCTL_TABLE().
- Patches 4-5 add register_sysctl() wrapper and update some callers.
- Patches 6-10 add register_net_sysctl[_sz]() wrappers and update some callers.
- Patches 11-13 add file2alias support.

Not all maintainers/reviewers (specially for the many changes in net/) are in To/Cc in respect of their time, as this RFC probably needs more general and earlier feedback before settling on specific changes for their review. All lists are included for visibility, though.

Example
=======

To put it all together, 'mpls_router.ko' (used above) from 'net/mpls/af_mpls.c':

- Tables:

        static const struct ctl_table mpls_table[] = {
                {
                        .procname       = "platform_labels",
                ...
                        .procname       = "ip_ttl_propagate",
                ...
                        .procname       = "default_ttl",
                },
        };
        
        static const struct ctl_table mpls_dev_table[] = {
                {
                        .procname       = "input",
                ...
                },
        };

- Registration:

        net->mpls.ctl = register_net_sysctl_sz(net, "net/mpls", table,
                                               table_size, mpls_table);

        #define path_template "net/mpls/conf/%s"
        ...
        mdev->sysctl = register_net_sysctl_sz(net, path, table, table_size,
                                              mpls_dev_table, path_template);

- Symbols:

        $ objdump -t net/mpls/mpls_router.ko \
          | grep '__mod_device_table__.*__sysctl__'
        0000000000000900 l     O .data  0000000000000018
                __mod_device_table__kmod_mpls_router__sysctl__mpls_table.177
        0000000000000940 l     O .data  0000000000000018
                __mod_device_table__kmod_mpls_router__sysctl__mpls_dev_table.174

- file2alias:

        $ grep '^MODULE_ALIAS("sysctl:' net/mpls/mpls_router.mod.c
        MODULE_ALIAS("sysctl:*/net/mpls/platform_labels");
        MODULE_ALIAS("sysctl:*/net/mpls/ip_ttl_propagate");
        MODULE_ALIAS("sysctl:*/net/mpls/default_ttl");
        MODULE_ALIAS("sysctl:*/net/mpls/conf/*/input");

- modinfo:

        $ modinfo ./mpls_router.ko  | grep sysctl:
        alias:          sysctl:*/net/mpls/conf/*/input
        alias:          sysctl:*/net/mpls/default_ttl
        alias:          sysctl:*/net/mpls/ip_ttl_propagate
        alias:          sysctl:*/net/mpls/platform_labels

Testing
=======

Configurations:
- allmodconfig with the option enabled (check for 'MODULE_ALIAS("sysctl:' lines)
- allmodconfig with the option disabled (check for code errors)
- allyesconfig with the option enabled (check for include errors)

Architectures (test different bitness, endianness, and ELF handling)
- x86_64, i386
- arm, arm64
- arc
- alpha
- loongarch
- m68k
- mips(64)(el)
- parisc(64)
- powerpc(64(le))
- riscv
- sparc64
- s390

The resulting '.mod.c' files of allmodconfig with the option enabled was checked for consistency across all architectures, and that the new aliases lines are the difference to allmodconfig with the option disabled.

Disabling
=========

- Per-call:

Use 'register_sysctl_sz()' or '__register_net_sysctl_sz()' directly.

- Per-file:

Use '#define SYSCTL_MODULE_ALIASES_DISABLE'.

- System-wide:

Maybe something along these lines:

        # cat /etc/modprobe.d/no-sysctl.conf
        alias sysctl:* no-sysctl
        install no-sysctl /bin/false
        # modinfo -F name sysctl:/proc/net/mpls/default_ttl
        mpls_router
        # modprobe sysctl:/proc/net/mpls/default_ttl
        modprobe: ERROR: Error running install command '/bin/false' for module no_sysctl: retcode 1
        modprobe: ERROR: could not insert 'no_sysctl': Invalid argument

P.S.
====

I wrote proof-of-concept patches for procps sysctl and systemd-sysctl some time ago, which worked as expected, i.e., successfully set sysctl tunables which did not exist in /proc/sys, by running 'modprobe sysctl:/proc/sys/...' and retrying.

Should this series eventually be merged, the patches will be submitted upstream as well, for userspace to start consuming it.

Changes in v3:
- All issues were reported by [email protected].
- Tested allmodconfig with option enabled on x86_64.
- Patch 2:
- Fix typo ('s/options/option/').
- Patch 3:
- Fix usage of __UNIQUE_ID().
- Add __must_be_array() in MODULE_SYSCTL_TABLE().
- Patch 4:
- Remove trailing backslash in register_sysctl().
- Patch 6:
- Fix missing __register_net_sysctl_sz() in !CONFIG_SYSCTL.
- Patch 13:
- Fix potential out-of-bounds read in do_sysctl_entry().
- Add check for entry size of zero.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- This is based on the series submitted 4 years ago,
with Originally-by: tags added in related patches.
- Link to v1: https://lore.kernel.org/linux-fsdevel/[email protected]/

---
Mauricio Faria de Oliveira (13):
keys, pidns, fs/verity, riscv/vector: reorder '#include <linux/sysctl.h>'
proc: add config option SYSCTL_MODULE_ALIASES
sysctl, mod_devicetable: add macro MODULE_SYSCTL_TABLE
sysctl: add register_sysctl() wrapper for MODULE_SYSCTL_TABLE
sysctl, parport: update register_sysctl() callers with template arguments sysctl, net: add register_net_sysctl{_sz}() wrappers for MODULE_SYSCTL_TABLE sysctl, net: update register_net_sysctl{_sz}() callers with template arguments sysctl, net: update register_net_sysctl_sz(ARRAY_SIZE(table_tmpl)) with template arguments sysctl, ipv6: update register_net_sysctl{_sz}() callers with template arguments sysctl, net: update register_net_sysctl_sz() edge case
sysctl: unrandomize struct ctl_table.procname
modpost: move addend_*_rel() calls into addend_rel()
modpost: handle MODULE_SYSCTL_TABLE symbols

 arch/riscv/kernel/vector.c              |   1 +
 drivers/net/vrf.c                       |   3 +-
 drivers/parport/procfs.c                |  26 +++-
 fs/proc/Kconfig                         |  13 ++
 fs/verity/init.c                        |   1 +
 include/linux/key.h                     |   1 -
 include/linux/mod_devicetable.h         |   7 +
 include/linux/pid_namespace.h           |   1 +
 include/linux/sysctl.h                  | 110 ++++++++++++++-
 include/net/ipv6.h                      |   6 +-
 include/net/net_namespace.h             |  47 ++++++-
 net/bridge/br_netfilter_hooks.c         |   3 +-
 net/core/neighbour.c                    |  11 +-
 net/core/sysctl_net_core.c              |   3 +-
 net/ieee802154/6lowpan/reassembly.c     |   2 +-
 net/ipv4/devinet.c                      |  12 +-
 net/ipv4/ip_fragment.c                  |   3 +-
 net/ipv4/route.c                        |   3 +-
 net/ipv4/sysctl_net_ipv4.c              |   2 +-
 net/ipv4/xfrm4_policy.c                 |   3 +-
 net/ipv6/addrconf.c                     |   7 +-
 net/ipv6/icmp.c                         |   6 +-
 net/ipv6/netfilter/nf_conntrack_reasm.c |   4 +-
 net/ipv6/reassembly.c                   |   3 +-
 net/ipv6/route.c                        |  10 +-
 net/ipv6/sysctl_net_ipv6.c              |  13 +-
 net/ipv6/xfrm6_policy.c                 |   3 +-
 net/mpls/af_mpls.c                      |   9 +-
 net/mptcp/ctrl.c                        |   3 +-
 net/netfilter/ipvs/ip_vs_ctl.c          |   4 +-
 net/netfilter/ipvs/ip_vs_lblc.c         |   3 +-
 net/netfilter/ipvs/ip_vs_lblcr.c        |   3 +-
 net/netfilter/nf_conntrack_standalone.c |   5 +-
 net/netfilter/nf_log.c                  |   9 +-
 net/rds/tcp.c                           |   4 +-
 net/sctp/sysctl.c                       |   3 +-
 net/smc/smc_sysctl.c                    |   2 +-
 net/sysctl_net.c                        |  10 +-
 net/unix/sysctl_net_unix.c              |   3 +-
 net/vmw_vsock/af_vsock.c                |   4 +-
 net/xfrm/xfrm_sysctl.c                  |   2 +-
 scripts/mod/devicetable-offsets.c       |   6 +
 scripts/mod/file2alias.c                | 237 ++++++++++++++++++++++++++++++++
 scripts/mod/modpost.c                   |  46 ++++---
 scripts/mod/modpost.h                   |  25 ++++
 45 files changed, 575 insertions(+), 107 deletions(-)

---
base-commit: 2697ef8943c9985c14708a6429e21812693857b2
change-id: 20260818-sysctl-module-aliases-2f5801b1eb71

Best regards,

Signature · 1 line
Mauricio Faria de Oliveira <[email protected]>