瀏覽代碼

Kernel+ping: Only allow superuser to create SOCK_RAW sockets

/bin/ping is now setuid-root, and will drop privileges immediately
after opening a raw socket.
Andreas Kling 5 年之前
父節點
當前提交
3f254bfbc8
共有 3 個文件被更改,包括 8 次插入0 次删除
  1. 2 0
      Kernel/Process.cpp
  2. 1 0
      Kernel/build-root-filesystem.sh
  3. 5 0
      Userland/ping.cpp

+ 2 - 0
Kernel/Process.cpp

@@ -2609,6 +2609,8 @@ size_t Process::amount_purgeable_nonvolatile() const
 
 
 int Process::sys$socket(int domain, int type, int protocol)
 int Process::sys$socket(int domain, int type, int protocol)
 {
 {
+    if ((type & SOCK_TYPE_MASK) == SOCK_RAW && !is_superuser())
+        return -EACCES;
     int fd = alloc_fd();
     int fd = alloc_fd();
     if (fd < 0)
     if (fd < 0)
         return fd;
         return fd;

+ 1 - 0
Kernel/build-root-filesystem.sh

@@ -82,6 +82,7 @@ else
 find ../Userland/ -type f -perm +111 -exec cp {} mnt/bin/ \;
 find ../Userland/ -type f -perm +111 -exec cp {} mnt/bin/ \;
 fi
 fi
 chmod 4755 mnt/bin/su
 chmod 4755 mnt/bin/su
+chmod 4755 mnt/bin/ping
 echo "done"
 echo "done"
 
 
 printf "installing applications... "
 printf "installing applications... "

+ 5 - 0
Userland/ping.cpp

@@ -37,6 +37,11 @@ int main(int argc, char** argv)
         return 1;
         return 1;
     }
     }
 
 
+    if (setgid(getgid()) || setuid(getuid())) {
+        fprintf(stderr, "Failed to drop privileges.\n");
+        return 1;
+    }
+
     struct timeval timeout {
     struct timeval timeout {
         1, 0
         1, 0
     };
     };