Re: [PATCH] SYS: add utsname_set_field to reuse code in setdomainname and sethostname

From: Bradley Morgan

Date: Mon Nov 09 2026 - 16:11:55 EST


On 9 November 2026 11:44:21 GMT, Laurijssen <lauserve@xxxxxxxxx> wrote:
>Changes after review, cleanup of duplicated code in sethostname and
>setdomainname. Moved reusable code to utsname_sysctl.c
>
>- add utsname_set_field to utsname_sysctl.c
>- call utsname_set_field from sethostname/setdomainname
>- moved uts_proc_notify from .h into utsname_sysctl.c and turned static

Hmm..

>
>Signed-off-by: Laurijssen <laurijssen@xxxxxxxxxxxxxx>
>---
> include/linux/utsname.h | 10 ++----
> kernel/sys.c | 46 ++-----------------------
> kernel/utsname_sysctl.c | 75 ++++++++++++++++++++++++++++++++++-------
> 3 files changed, 66 insertions(+), 65 deletions(-)
>
>diff --git a/include/linux/utsname.h b/include/linux/utsname.h
>index dc5594ffffcc..409261d5381f 100644
>--- a/include/linux/utsname.h
>+++ b/include/linux/utsname.h
>@@ -18,14 +18,6 @@ enum uts_proc {
> UTS_PROC_DOMAINNAME,
> };
>
>-#ifdef CONFIG_SYSCTL
>-extern void uts_proc_notify(enum uts_proc proc);
>-#else
>-static inline void uts_proc_notify(enum uts_proc proc)
>-{
>-}
>-#endif
>-
> static inline struct new_utsname *utsname(void)
> {
> return &current->nsproxy->uts_ns->name;
>@@ -38,4 +30,6 @@ static inline struct new_utsname *init_utsname(void)
>
> extern struct rw_semaphore uts_sem;
>
>+int utsname_set_field(char __user *name, int len, enum uts_proc proc);
>+
> #endif /* _LINUX_UTSNAME_H */
>diff --git a/kernel/sys.c b/kernel/sys.c
>index 755bfa5ccd92..688c5e9e222f 100644
>--- a/kernel/sys.c
>+++ b/kernel/sys.c
>@@ -1421,28 +1421,7 @@ SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
>
> SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
> {
>- int errno;
>- char tmp[__NEW_UTS_LEN];
>-
>- if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
>- return -EPERM;
>-
>- if (len < 0 || len > __NEW_UTS_LEN)
>- return -EINVAL;
>- errno = -EFAULT;
>- if (!copy_from_user(tmp, name, len)) {
>- struct new_utsname *u;
>-
>- add_device_randomness(tmp, len);
>- down_write(&uts_sem);
>- u = utsname();
>- memcpy(u->nodename, tmp, len);
>- memset(u->nodename + len, 0, sizeof(u->nodename) - len);
>- errno = 0;
>- uts_proc_notify(UTS_PROC_HOSTNAME);
>- up_write(&uts_sem);
>- }
>- return errno;
>+ return utsname_set_field(name, len, UTS_PROC_HOSTNAME);
> }
>
> #ifdef __ARCH_WANT_SYS_GETHOSTNAME
>@@ -1475,28 +1454,7 @@ SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
> */
> SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
> {
>- int errno;
>- char tmp[__NEW_UTS_LEN];
>-
>- if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
>- return -EPERM;
>- if (len < 0 || len > __NEW_UTS_LEN)
>- return -EINVAL;
>-
>- errno = -EFAULT;
>- if (!copy_from_user(tmp, name, len)) {
>- struct new_utsname *u;
>-
>- add_device_randomness(tmp, len);
>- down_write(&uts_sem);
>- u = utsname();
>- memcpy(u->domainname, tmp, len);
>- memset(u->domainname + len, 0, sizeof(u->domainname) - len);
>- errno = 0;
>- uts_proc_notify(UTS_PROC_DOMAINNAME);
>- up_write(&uts_sem);
>- }
>- return errno;
>+ return utsname_set_field(name, len, UTS_PROC_DOMAINNAME);
> }
>
> /* make sure you are allowed to change @tsk limits before calling this */
>diff --git a/kernel/utsname_sysctl.c b/kernel/utsname_sysctl.c
>index 6c1522201e39..026e5c877a8b 100644
>--- a/kernel/utsname_sysctl.c
>+++ b/kernel/utsname_sysctl.c
>@@ -5,6 +5,7 @@
> * Author: Eric Biederman <ebiederm@xxxxxxxxxxx>
> */
>
>+#include <linux/capability.h>
> #include <linux/export.h>
> #include <linux/uts.h>
> #include <linux/utsname.h>
>@@ -12,6 +13,9 @@
> #include <linux/sysctl.h>
> #include <linux/wait.h>
> #include <linux/rwsem.h>
>+#include <linux/uaccess.h>
>+
>+static const struct ctl_table uts_kern_table[];
>
> #ifdef CONFIG_SYSCTL
>
>@@ -67,10 +71,68 @@ static int proc_do_uts_string(const struct ctl_table *table, int write,
>
> return r;
> }
>+
>+/*
>+ * Notify userspace about a change in a certain entry of uts_kern_table,
>+ * identified by the parameter proc.
>+ */
>+static void uts_proc_notify(enum uts_proc proc)
>+{
>+ const struct ctl_table *table = &uts_kern_table[proc];
>+
>+ proc_sys_poll_notify(table->poll);
>+}
> #else
> #define proc_do_uts_string NULL
>+
>+static inline void uts_proc_notify(enum uts_proc proc)
>+{
>+}
> #endif
>
>+int utsname_set_field(char __user *name, int len, enum uts_proc proc)
>+{
>+ int errno;
>+ char tmp[__NEW_UTS_LEN];
>+
>+ if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
>+ return -EPERM;
>+
>+ if (len < 0 || len > __NEW_UTS_LEN)
>+ return -EINVAL;
>+ errno = -EFAULT;
>+ if (!copy_from_user(tmp, name, len)) {
>+ struct new_utsname *u;
>+ char *field;
>+
>+ add_device_randomness(tmp, len);
>+ down_write(&uts_sem);
>+
>+ u = utsname();
>+
>+ switch (proc) {
>+ case UTS_PROC_HOSTNAME:
>+ field = u->nodename;
>+ break;
>+ case UTS_PROC_DOMAINNAME:
>+ field = u->domainname;
>+ break;
>+ default:
>+ WARN_ON_ONCE(1);
>+ field = u->domainname;
>+ break;
>+ }
>+
>+ memcpy(field, tmp, len);
>+ memset(field + len, 0, sizeof_field(struct new_utsname, nodename) - len);
>+
>+ errno = 0;
>+ uts_proc_notify(proc);
>+ up_write(&uts_sem);
>+ }
>+ return errno;
>+}
>+
> static DEFINE_CTL_TABLE_POLL(hostname_poll);
> static DEFINE_CTL_TABLE_POLL(domainname_poll);
>
>@@ -122,19 +184,6 @@ static const struct ctl_table uts_kern_table[] = {
> },
> };
>
>-#ifdef CONFIG_SYSCTL
>-/*
>- * Notify userspace about a change in a certain entry of uts_kern_table,
>- * identified by the parameter proc.
>- */
>-void uts_proc_notify(enum uts_proc proc)
>-{
>- const struct ctl_table *table = &uts_kern_table[proc];
>-
>- proc_sys_poll_notify(table->poll);
>-}
>-#endif
>-
> static int __init utsname_sysctl_init(void)
> {
> register_sysctl("kernel", uts_kern_table);

This code doesn't compile, please check kernel test robot.

>


--- Thanks!