mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 2.5.66 & 2.4.20] Fix sys_sethostname() and sys_setdomainname()
@ 2003-03-30 20:05 Stephan Maciej
  0 siblings, 0 replies; only message in thread
From: Stephan Maciej @ 2003-03-30 20:05 UTC (permalink / raw)
  To: Linux Kernel Mailing List; +Cc: Linus Torvalds, Marcelo Tosatti

[-- 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;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2003-03-30 23:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-30 20:05 [PATCH 2.5.66 & 2.4.20] Fix sys_sethostname() and sys_setdomainname() Stephan Maciej

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®