Selaa lähdekoodia

Add uid and gid to CharacterDevices.

The vast majority of them will be owned by 0:0 (the default.)
However, PTY pairs will now be owned by the uid:gid of the opening process.
Andreas Kling 6 vuotta sitten
vanhempi
commit
34e745b0b4

+ 7 - 0
Kernel/CharacterDevice.h

@@ -32,10 +32,17 @@ public:
 
 
     virtual const char* class_name() const = 0;
     virtual const char* class_name() const = 0;
 
 
+    uid_t uid() const { return m_uid; }
+    uid_t gid() const { return m_gid; }
+
 protected:
 protected:
     CharacterDevice(unsigned major, unsigned minor) : m_major(major), m_minor(minor) { }
     CharacterDevice(unsigned major, unsigned minor) : m_major(major), m_minor(minor) { }
+    void set_uid(uid_t uid) { m_uid = uid; }
+    void set_gid(gid_t gid) { m_gid = gid; }
 
 
 private:
 private:
     unsigned m_major { 0 };
     unsigned m_major { 0 };
     unsigned m_minor { 0 };
     unsigned m_minor { 0 };
+    uid_t m_uid { 0 };
+    gid_t m_gid { 0 };
 };
 };

+ 8 - 5
Kernel/DevPtsFS.cpp

@@ -44,12 +44,15 @@ RetainPtr<SynthFSInode> DevPtsFS::create_slave_pty_device_file(unsigned index)
     builder.appendf("%u", index);
     builder.appendf("%u", index);
     file->m_name = builder.build();
     file->m_name = builder.build();
 
 
+    auto* device = VFS::the().get_device(11, index);
+    ASSERT(device);
+
     file->m_metadata.size = 0;
     file->m_metadata.size = 0;
-    file->m_metadata.uid = 0;
-    file->m_metadata.gid = 0;
-    file->m_metadata.mode = 0020666;
-    file->m_metadata.majorDevice = 11;
-    file->m_metadata.minorDevice = index;
+    file->m_metadata.uid = device->uid();
+    file->m_metadata.gid = device->gid();
+    file->m_metadata.mode = 0020644;
+    file->m_metadata.majorDevice = device->major();
+    file->m_metadata.minorDevice = device->minor();
     file->m_metadata.mtime = mepoch;
     file->m_metadata.mtime = mepoch;
     return file;
     return file;
 }
 }

+ 3 - 0
Kernel/MasterPTY.cpp

@@ -1,6 +1,7 @@
 #include "MasterPTY.h"
 #include "MasterPTY.h"
 #include "SlavePTY.h"
 #include "SlavePTY.h"
 #include "PTYMultiplexer.h"
 #include "PTYMultiplexer.h"
+#include <Kernel/Process.h>
 #include <LibC/errno_numbers.h>
 #include <LibC/errno_numbers.h>
 
 
 MasterPTY::MasterPTY(unsigned index)
 MasterPTY::MasterPTY(unsigned index)
@@ -8,6 +9,8 @@ MasterPTY::MasterPTY(unsigned index)
     , m_slave(adopt(*new SlavePTY(*this, index)))
     , m_slave(adopt(*new SlavePTY(*this, index)))
     , m_index(index)
     , m_index(index)
 {
 {
+    set_uid(current->uid());
+    set_gid(current->gid());
 }
 }
 
 
 MasterPTY::~MasterPTY()
 MasterPTY::~MasterPTY()

+ 1 - 0
Kernel/PTYMultiplexer.cpp

@@ -1,5 +1,6 @@
 #include "PTYMultiplexer.h"
 #include "PTYMultiplexer.h"
 #include "MasterPTY.h"
 #include "MasterPTY.h"
+#include <Kernel/Process.h>
 #include <LibC/errno_numbers.h>
 #include <LibC/errno_numbers.h>
 
 
 static const unsigned s_max_pty_pairs = 8;
 static const unsigned s_max_pty_pairs = 8;

+ 3 - 0
Kernel/SlavePTY.cpp

@@ -1,12 +1,15 @@
 #include "SlavePTY.h"
 #include "SlavePTY.h"
 #include "MasterPTY.h"
 #include "MasterPTY.h"
 #include "DevPtsFS.h"
 #include "DevPtsFS.h"
+#include <Kernel/Process.h>
 
 
 SlavePTY::SlavePTY(MasterPTY& master, unsigned index)
 SlavePTY::SlavePTY(MasterPTY& master, unsigned index)
     : TTY(11, index)
     : TTY(11, index)
     , m_master(master)
     , m_master(master)
     , m_index(index)
     , m_index(index)
 {
 {
+    set_uid(current->uid());
+    set_gid(current->gid());
     VFS::the().register_character_device(*this);
     VFS::the().register_character_device(*this);
     DevPtsFS::the().register_slave_pty(*this);
     DevPtsFS::the().register_slave_pty(*this);
     set_size(80, 25);
     set_size(80, 25);

+ 8 - 0
Kernel/VirtualFileSystem.cpp

@@ -517,6 +517,14 @@ void VFS::unregister_character_device(CharacterDevice& device)
     m_character_devices.remove(encodedDevice(device.major(), device.minor()));
     m_character_devices.remove(encodedDevice(device.major(), device.minor()));
 }
 }
 
 
+CharacterDevice* VFS::get_device(unsigned major, unsigned minor)
+{
+    auto it = m_character_devices.find(encodedDevice(major, minor));
+    if (it == m_character_devices.end())
+        return nullptr;
+    return (*it).value;
+}
+
 void VFS::for_each_mount(Function<void(const Mount&)> callback) const
 void VFS::for_each_mount(Function<void(const Mount&)> callback) const
 {
 {
     for (auto& mount : m_mounts) {
     for (auto& mount : m_mounts) {

+ 2 - 0
Kernel/VirtualFileSystem.h

@@ -86,6 +86,8 @@ public:
 
 
     void sync();
     void sync();
 
 
+    CharacterDevice* get_device(unsigned major, unsigned minor);
+
 private:
 private:
     friend class FileDescriptor;
     friend class FileDescriptor;