Kernel/LibC/Userspace: Implement chown and set tty owner on login

This commit is contained in:
Bananymous 2024-01-02 22:19:38 +02:00
parent 07d5d3f936
commit 96d831c31a
12 changed files with 62 additions and 2 deletions

View File

@ -101,6 +101,7 @@ namespace Kernel
BAN::ErrorOr<size_t> write(off_t, BAN::ConstByteSpan buffer); BAN::ErrorOr<size_t> write(off_t, BAN::ConstByteSpan buffer);
BAN::ErrorOr<void> truncate(size_t); BAN::ErrorOr<void> truncate(size_t);
BAN::ErrorOr<void> chmod(mode_t); BAN::ErrorOr<void> chmod(mode_t);
BAN::ErrorOr<void> chown(uid_t, gid_t);
bool has_data() const; bool has_data() const;
protected: protected:
@ -119,6 +120,7 @@ namespace Kernel
virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); } virtual BAN::ErrorOr<size_t> write_impl(off_t, BAN::ConstByteSpan) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); } virtual BAN::ErrorOr<void> truncate_impl(size_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> chmod_impl(mode_t) { return BAN::Error::from_errno(ENOTSUP); } virtual BAN::ErrorOr<void> chmod_impl(mode_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual BAN::ErrorOr<void> chown_impl(uid_t, gid_t) { return BAN::Error::from_errno(ENOTSUP); }
virtual bool has_data_impl() const { dwarnln("nonblock not supported"); return true; } virtual bool has_data_impl() const { dwarnln("nonblock not supported"); return true; }
protected: protected:

View File

@ -108,6 +108,7 @@ namespace Kernel
BAN::ErrorOr<long> sys_pread(int fd, void* buffer, size_t count, off_t offset); BAN::ErrorOr<long> sys_pread(int fd, void* buffer, size_t count, off_t offset);
BAN::ErrorOr<long> sys_chmod(const char*, mode_t); BAN::ErrorOr<long> sys_chmod(const char*, mode_t);
BAN::ErrorOr<long> sys_chown(const char*, uid_t, gid_t);
BAN::ErrorOr<long> sys_pipe(int fildes[2]); BAN::ErrorOr<long> sys_pipe(int fildes[2]);
BAN::ErrorOr<long> sys_dup(int fildes); BAN::ErrorOr<long> sys_dup(int fildes);

View File

@ -41,6 +41,9 @@ namespace Kernel
virtual void clear() = 0; virtual void clear() = 0;
virtual BAN::ErrorOr<void> chmod_impl(mode_t) override;
virtual BAN::ErrorOr<void> chown_impl(uid_t, gid_t) override;
virtual bool has_data_impl() const override; virtual bool has_data_impl() const override;
protected: protected:

View File

@ -141,6 +141,12 @@ namespace Kernel
return chmod_impl(mode); return chmod_impl(mode);
} }
BAN::ErrorOr<void> Inode::chown(uid_t uid, gid_t gid)
{
LockGuard _(m_lock);
return chown_impl(uid, gid);
}
bool Inode::has_data() const bool Inode::has_data() const
{ {
LockGuard _(m_lock); LockGuard _(m_lock);

View File

@ -886,6 +886,18 @@ namespace Kernel
return 0; return 0;
} }
BAN::ErrorOr<long> Process::sys_chown(const char* path, uid_t uid, gid_t gid)
{
LockGuard _(m_lock);
validate_string_access(path);
auto absolute_path = TRY(absolute_path_of(path));
auto file = TRY(VirtualFileSystem::get().file_from_absolute_path(m_credentials, absolute_path, O_WRONLY));
TRY(file.inode->chown(uid, gid));
return 0;
}
BAN::ErrorOr<long> Process::sys_pipe(int fildes[2]) BAN::ErrorOr<long> Process::sys_pipe(int fildes[2])
{ {
LockGuard _(m_lock); LockGuard _(m_lock);

View File

@ -211,6 +211,9 @@ namespace Kernel
case SYS_PREAD: case SYS_PREAD:
ret = Process::current().sys_pread((int)arg1, (void*)arg2, (size_t)arg3, (off_t)arg4); ret = Process::current().sys_pread((int)arg1, (void*)arg2, (size_t)arg3, (off_t)arg4);
break; break;
case SYS_CHOWN:
ret = Process::current().sys_chown((const char*)arg1, (uid_t)arg2, (gid_t)arg3);
break;
default: default:
dwarnln("Unknown syscall {}", syscall); dwarnln("Unknown syscall {}", syscall);
break; break;

View File

@ -179,7 +179,7 @@ namespace Kernel
} }
SerialTTY::SerialTTY(Serial serial) SerialTTY::SerialTTY(Serial serial)
: TTY(0660, 0, 0) : TTY(0600, 0, 0)
, m_serial(serial) , m_serial(serial)
, m_rdev(next_rdev()) , m_rdev(next_rdev())
{ {

View File

@ -105,6 +105,21 @@ namespace Kernel
initialized = true; initialized = true;
} }
BAN::ErrorOr<void> TTY::chmod_impl(mode_t mode)
{
ASSERT((mode & Inode::Mode::TYPE_MASK) == 0);
m_inode_info.mode &= Inode::Mode::TYPE_MASK;
m_inode_info.mode |= mode;
return {};
}
BAN::ErrorOr<void> TTY::chown_impl(uid_t uid, gid_t gid)
{
m_inode_info.uid = uid;
m_inode_info.gid = gid;
return {};
}
void TTY::on_key_event(Input::KeyEvent event) void TTY::on_key_event(Input::KeyEvent event)
{ {
LockGuard _(m_lock); LockGuard _(m_lock);

View File

@ -42,7 +42,7 @@ namespace Kernel
} }
VirtualTTY::VirtualTTY(TerminalDriver* driver) VirtualTTY::VirtualTTY(TerminalDriver* driver)
: TTY(0666, 0, 0) : TTY(0600, 0, 0)
, m_terminal_driver(driver) , m_terminal_driver(driver)
, m_rdev(next_rdev()) , m_rdev(next_rdev())
{ {

View File

@ -61,6 +61,7 @@ __BEGIN_DECLS
#define SYS_READLINKAT 60 #define SYS_READLINKAT 60
#define SYS_MSYNC 61 #define SYS_MSYNC 61
#define SYS_PREAD 62 #define SYS_PREAD 62
#define SYS_CHOWN 63
__END_DECLS __END_DECLS

View File

@ -270,6 +270,11 @@ int chdir(const char* path)
return syscall(SYS_SET_PWD, path); return syscall(SYS_SET_PWD, path);
} }
int chown(const char* path, uid_t owner, gid_t group)
{
return syscall(SYS_CHOWN, path, owner, group);
}
void sync(void) void sync(void)
{ {
syscall(SYS_SYNC, false); syscall(SYS_SYNC, false);

View File

@ -63,6 +63,18 @@ int main()
if (pwd == nullptr) if (pwd == nullptr)
continue; continue;
if (chown("/dev/tty", pwd->pw_uid, pwd->pw_gid) == -1)
{
perror("chown");
continue;
}
if (chmod("/dev/tty", 0600) == -1)
{
perror("chmod");
continue;
}
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) if (pid == 0)
{ {