mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stephan Maciej <stephanm@muc.de>
To: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Cc: Linus Torvalds <torvalds@transmeta.com>,
	Marcelo Tosatti <marcelo@conectiva.com.br>
Subject: [PATCH 2.5.66 & 2.4.20] Fix sys_sethostname() and sys_setdomainname()
Date: Sun, 30 Mar 2003 22:05:06 +0200	[thread overview]
Message-ID: <200303302205.06770.stephanm@muc.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 494 bytes --]

Hi,

a long time ago someone mentioned that sethostname() and setdomainname() 
properly return -EFAULT when passing an invalid pointer to the new name, but 
instead of leaving the fields in system_utsname unchanged the nodename and 
domainname are set to an empty string.

The behaviour of sys_sethostname() can be verified using the attached program 
(sethostname.c); sys_setdomainname() is almost the same so I guess that it 
has the same defect :-)

The attached patches fix this. 

Stephan

[-- Attachment #2: sethostanddomainname-2.5.66.diff --]
[-- Type: text/x-diff, Size: 1106 bytes --]

--- kernel/sys.c~unmodified	2003-03-30 21:33:17.000000000 +0200
+++ kernel/sys.c	2003-03-30 21:43:20.000000000 +0200
@@ -1154,14 +1154,18 @@
 asmlinkage long sys_sethostname(char *name, int len)
 {
 	int errno;
+	char tmp[__NEW_UTS_LEN];
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
 	if (len < 0 || len > __NEW_UTS_LEN)
 		return -EINVAL;
+	
 	down_write(&uts_sem);
 	errno = -EFAULT;
-	if (!copy_from_user(system_utsname.nodename, name, len)) {
+
+	if (!copy_from_user(tmp, name, len)) {
+		memcpy(system_utsname.nodename, tmp, len);
 		system_utsname.nodename[len] = 0;
 		errno = 0;
 	}
@@ -1193,6 +1197,7 @@
 asmlinkage long sys_setdomainname(char *name, int len)
 {
 	int errno;
+	char tmp[__NEW_UTS_LEN];
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -1201,10 +1206,13 @@
 
 	down_write(&uts_sem);
 	errno = -EFAULT;
-	if (!copy_from_user(system_utsname.domainname, name, len)) {
-		errno = 0;
+
+	if (!copy_from_user(tmp, name, len)) {
+		memcpy(system_utsname.domainname, tmp, len);
 		system_utsname.domainname[len] = 0;
+		errno = 0;
 	}
+
 	up_write(&uts_sem);
 	return errno;
 }

[-- Attachment #3: sethostname.c --]
[-- Type: text/x-csrc, Size: 944 bytes --]


#include <linux/utsname.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int main()
{
  int retval;
  char hostname[__NEW_UTS_LEN];

  // Print out current hostname
  if ((retval = gethostname(hostname, __NEW_UTS_LEN)) == -1) {
    printf("call to gethostname() failed, retval=%d: %s\n", retval, strerror(errno));
    return 1;
  };
  
  printf("gethostname()=\"%s\"\n", hostname);

  // Set "new" hostname
  if ((retval = sethostname((char *)-1, 10)) != -1) {
    // We really EXPECT this to fail!
    printf("The Penguin is in trouble!\n");
    return 2;
  } else
    printf("sethostname(INVALID, 10)=%d (error \"%s\")\n", retval, strerror(errno));

  // And now once again:
  if ((retval = gethostname(hostname, __NEW_UTS_LEN)) == -1) {
    printf("call to gethostname() failed, retval=%d: %s\n", retval, strerror(errno));
    return 1;
  };
  
  printf("gethostname()=\"%s\"\n", hostname);

  return 0;
};

[-- Attachment #4: sethostanddomainname-2.4.20.diff --]
[-- Type: text/x-diff, Size: 1075 bytes --]

--- kernel/sys.c~unmodified	2003-03-30 21:51:07.000000000 +0200
+++ kernel/sys.c	2003-03-30 21:53:01.000000000 +0200
@@ -1026,6 +1026,7 @@
 asmlinkage long sys_sethostname(char *name, int len)
 {
 	int errno;
+	char tmp[__NEW_UTS_LEN];
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -1033,7 +1034,8 @@
 		return -EINVAL;
 	down_write(&uts_sem);
 	errno = -EFAULT;
-	if (!copy_from_user(system_utsname.nodename, name, len)) {
+	if (!copy_from_user(tmp, name, len)) {
+		memcpy(system_utsname.nodename, tmp, len);
 		system_utsname.nodename[len] = 0;
 		errno = 0;
 	}
@@ -1065,6 +1067,7 @@
 asmlinkage long sys_setdomainname(char *name, int len)
 {
 	int errno;
+	char tmp[__NEW_UTS_LEN];
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -1073,9 +1076,10 @@
 
 	down_write(&uts_sem);
 	errno = -EFAULT;
-	if (!copy_from_user(system_utsname.domainname, name, len)) {
-		errno = 0;
+	if (!copy_from_user(tmp, name, len)) {
+		memcpy(system_utsname.domainname, tmp, len);
 		system_utsname.domainname[len] = 0;
+		errno = 0;
 	}
 	up_write(&uts_sem);
 	return errno;

                 reply	other threads:[~2003-03-30 23:34 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=200303302205.06770.stephanm@muc.de \
    --to=stephanm@muc.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marcelo@conectiva.com.br \
    --cc=torvalds@transmeta.com \
    /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®