Explorar o código

Kernel+LibC: Add O_EXEC, move exec permission checking to VFS::open()

O_EXEC is mentioned by POSIX, so let's have it. Currently, it is only used
inside the kernel to ensure the process has the right permissions when opening
an executable.
Sergey Bugaev %!s(int64=5) %!d(string=hai) anos
pai
achega
2fcbb846fb

+ 4 - 0
Kernel/FileSystem/VirtualFileSystem.cpp

@@ -215,6 +215,10 @@ KResultOr<NonnullRefPtr<FileDescription>> VFS::open(StringView path, int options
             return KResult(-EISDIR);
         should_truncate_file = options & O_TRUNC;
     }
+    if (options & O_EXEC) {
+        if (!metadata.may_execute(current->process()))
+            return KResult(-EACCES);
+    }
 
     if (metadata.is_device()) {
         auto device = Device::get_device(metadata.major_device, metadata.minor_device);

+ 1 - 0
Kernel/FileSystem/VirtualFileSystem.h

@@ -15,6 +15,7 @@
 #define O_RDONLY 0
 #define O_WRONLY 1
 #define O_RDWR 2
+#define O_EXEC 4
 #define O_CREAT 0100
 #define O_EXCL 0200
 #define O_NOCTTY 0400

+ 1 - 4
Kernel/Process.cpp

@@ -653,15 +653,12 @@ int Process::do_exec(String path, Vector<String> arguments, Vector<String> envir
     if (parts.is_empty())
         return -ENOENT;
 
-    auto result = VFS::the().open(path, 0, 0, current_directory());
+    auto result = VFS::the().open(path, O_EXEC, 0, current_directory());
     if (result.is_error())
         return result.error();
     auto description = result.value();
     auto metadata = description->metadata();
 
-    if (!metadata.may_execute(*this))
-        return -EACCES;
-
     if (!metadata.size)
         return -ENOTIMPL;
 

+ 1 - 0
Libraries/LibC/fcntl.h

@@ -17,6 +17,7 @@ __BEGIN_DECLS
 #define O_WRONLY 1
 #define O_RDWR 2
 #define O_ACCMODE 3
+#define O_EXEC 4
 #define O_CREAT 0100
 #define O_EXCL 0200
 #define O_NOCTTY 0400