Skip to contentSkip to conversation

Fix sys_sethostname() and sys_setdomainname()

PATCH New Stephan Maciej 1 participant 1 message Mar 30, 2003

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix sys_sethostname() and sys_setdomainname()

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

sys.c renamed from sys.c~unmodified +11 -3
@@ -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;
 }

#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;
};

sys.c renamed from sys.c~unmodified +7 -3
@@ -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;