From: Laurijssen <lauserve@gmail.com>
To: linux-kernel@vger.kernel.org, pjw@kernel.org,
akpm@linux-foundation.org, jannh@google.com, brauner@kernel.org
Cc: Laurijssen <laurijssen@protonmail.com>
Subject: [PATCH] add utsname_set_field to reuse code in setdomainname and sethostname
Date: Thu, 5 Nov 2026 13:07:51 +0100 [thread overview]
Message-ID: <20261105120751.920311-1-laurijssen@protonmail.com> (raw)
- add utsname_set_field to utsname_sysctl.c, it reuses duplicated code
- call utsname_set_field from sethostname/setdomainname
- moved uts_proc_notify from .h into utsname_sysctl.c and turned static
Signed-off-by: Laurijssen <laurijssen@protonmail.com>
---
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 ¤t->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@xmision.com>
*/
+#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);
--
2.53.0
next reply other threads:[~2026-11-05 12:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-05 12:07 Laurijssen [this message]
2026-11-06 15:56 ` kernel test robot
2026-11-06 16:31 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261105120751.920311-1-laurijssen@protonmail.com \
--to=lauserve@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=brauner@kernel.org \
--cc=jannh@google.com \
--cc=laurijssen@protonmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=pjw@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®