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 setutsname to reuse code in setdomainname and sethostname
Date: Sun, 1 Nov 2026 08:12:27 +0100 [thread overview]
Message-ID: <20261101071227.14893-1-laurijssen@protonmail.com> (raw)
- sethostname and setdomainname duplicated code with one field difference.
- added setutsname with arguments domainname/hostname to reuse the same code.
Signed-off-by: Laurijssen <laurijssen@protonmail.com>
---
kernel/sys.c | 46 ++++++++++++++++++----------------------------
1 file changed, 18 insertions(+), 28 deletions(-)
diff --git a/kernel/sys.c b/kernel/sys.c
index 755bfa5ccd92..3a4e483c22d5 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -1419,7 +1419,9 @@ SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
}
#endif
-SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
+static int setutsname(char __user *name, int len, enum uts_proc uts_proc,
+ char (*field)[__NEW_UTS_LEN + 1],
+ int fieldlen)
{
int errno;
char tmp[__NEW_UTS_LEN];
@@ -1431,20 +1433,26 @@ SYSCALL_DEFINE2(sethostname, char __user *, name, int, 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);
+ memcpy(*field, tmp, len);
+ memset(*field + len, 0, fieldlen - len);
+
errno = 0;
- uts_proc_notify(UTS_PROC_HOSTNAME);
+ uts_proc_notify(uts_proc);
up_write(&uts_sem);
}
return errno;
}
+SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
+{
+ // utsname() always returns a valid pointer to the current utsname structure.
+ struct new_utsname *u = utsname();
+
+ return setutsname(name, len, UTS_PROC_HOSTNAME, &u->nodename, sizeof(u->nodename));
+}
+
#ifdef __ARCH_WANT_SYS_GETHOSTNAME
SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
@@ -1475,28 +1483,10 @@ 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;
+ // utsname() always returns a valid pointer to the current utsname structure.
+ struct new_utsname *u = utsname();
- 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 setutsname(name, len, UTS_PROC_DOMAINNAME, &u->domainname, sizeof(u->domainname));
}
/* make sure you are allowed to change @tsk limits before calling this */
--
2.53.0
next reply other threads:[~2026-11-01 7:12 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-01 7:12 Laurijssen [this message]
2026-11-01 15:38 ` Bradley Morgan
2026-11-09 11:44 ` [PATCH] SYS: add utsname_set_field " Laurijssen
2026-11-09 20:30 ` 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=20261101071227.14893-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®