From 7c4a4ac0bab5ba7782547da0ea7ab53bdd3a2482 Mon Sep 17 00:00:00 2001 From: Enrico Fraccaroli Date: Mon, 27 Dec 2021 00:49:28 +0100 Subject: [PATCH] Fix difference between PGID and GID. Correctly set UID and GID of new processes. Implement set/get PGID and GID. --- libc/CMakeLists.txt | 4 ++ libc/inc/fcntl.h | 29 ++++++----- libc/inc/sys/unistd.h | 23 +++++++++ libc/src/unistd/getpgid.c | 11 ++++ libc/src/unistd/getuid.c | 11 ++++ libc/src/unistd/setpgid.c | 11 ++++ libc/src/unistd/setuid.c | 11 ++++ mentos/inc/fs/ext2.h | 2 - mentos/inc/process/process.h | 6 ++- mentos/inc/system/syscall.h | 25 ++++++++- mentos/src/fs/ext2.c | 44 +++++++--------- mentos/src/kernel.c | 2 - mentos/src/process/process.c | 15 +++--- mentos/src/process/scheduler.c | 94 ++++++++++++++++++++++------------ mentos/src/system/signal.c | 2 +- mentos/src/system/syscall.c | 8 ++- programs/login.c | 3 ++ programs/mkdir.c | 3 +- programs/shell.c | 2 +- programs/touch.c | 2 +- 20 files changed, 216 insertions(+), 92 deletions(-) create mode 100644 libc/src/unistd/getpgid.c create mode 100644 libc/src/unistd/getuid.c create mode 100644 libc/src/unistd/setpgid.c create mode 100644 libc/src/unistd/setuid.c diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 8b66e9c..0d26c45 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -89,8 +89,12 @@ add_library( ${PROJECT_SOURCE_DIR}/src/unistd/exit.c ${PROJECT_SOURCE_DIR}/src/unistd/setsid.c ${PROJECT_SOURCE_DIR}/src/unistd/getsid.c + ${PROJECT_SOURCE_DIR}/src/unistd/setpgid.c + ${PROJECT_SOURCE_DIR}/src/unistd/getpgid.c ${PROJECT_SOURCE_DIR}/src/unistd/setgid.c ${PROJECT_SOURCE_DIR}/src/unistd/getgid.c + ${PROJECT_SOURCE_DIR}/src/unistd/setuid.c + ${PROJECT_SOURCE_DIR}/src/unistd/getuid.c ${PROJECT_SOURCE_DIR}/src/unistd/fork.c ${PROJECT_SOURCE_DIR}/src/unistd/read.c ${PROJECT_SOURCE_DIR}/src/unistd/write.c diff --git a/libc/inc/fcntl.h b/libc/inc/fcntl.h index 255ffd3..3c965dc 100644 --- a/libc/inc/fcntl.h +++ b/libc/inc/fcntl.h @@ -20,20 +20,21 @@ /// @brief The file modes. /// @{ -#define S_IRWXU 0000700U ///< RWX mask for user. -#define S_IRUSR 0000400U ///< R for user. -#define S_IWUSR 0000200U ///< W for user. -#define S_IXUSR 0000100U ///< X for user. - -#define S_IRWXG 0000070U ///< RWX mask for group. -#define S_IRGRP 0000040U ///< R for group. -#define S_IWGRP 0000020U ///< W for group. -#define S_IXGRP 0000010U ///< X for group. - -#define S_IRWXO 0000007U ///< RWX mask for group. -#define S_IROTH 0000004U ///< R for group. -#define S_IWOTH 0000002U ///< W for group. -#define S_IXOTH 0000001U ///< X for group. +#define S_ISUID 0x0800 ///< Set user id on execution +#define S_ISGID 0x0400 ///< Set group id on execution +#define S_ISVTX 0x0200 ///< Save swapped text even after use (Sticky Bit) +#define S_IRWXU 0x01C0 ///< rwx------- : User can read/write/execute +#define S_IRUSR 0x0100 ///< -r-------- : User can read +#define S_IWUSR 0x0080 ///< --w------- : User can write +#define S_IXUSR 0x0040 ///< ---x------ : User can execute +#define S_IRWXG 0x0038 ///< ----rwx--- : Group can read/write/execute +#define S_IRGRP 0x0020 ///< ----r----- : Group can read +#define S_IWGRP 0x0010 ///< -----w---- : Group can write +#define S_IXGRP 0x0008 ///< ------x--- : Group can execute +#define S_IRWXO 0x0007 ///< -------rwx : Others can read/write/execute +#define S_IROTH 0x0004 ///< -------r-- : Others can read +#define S_IWOTH 0x0002 ///< --------w- : Others can write +#define S_IXOTH 0x0001 ///< ---------x : Others can execute #define S_ISDIR(m) (((m)&0170000) == 0040000) ///< directory. #define S_ISCHR(m) (((m)&0170000) == 0020000) ///< char special diff --git a/libc/inc/sys/unistd.h b/libc/inc/sys/unistd.h index b305d0f..63bf18c 100644 --- a/libc/inc/sys/unistd.h +++ b/libc/inc/sys/unistd.h @@ -84,6 +84,19 @@ extern pid_t getsid(pid_t pid); /// Otherwise return -1 with errno : EPERM extern pid_t setsid(); +///@brief returns the Process Group ID (PGID) of the process specified by pid. +/// If pid is zero, the process ID of the calling process is used. +/// @param pid process of which we want to know the PGID. +/// @return the PGID of the specified process. +pid_t getpgid(pid_t pid); + +/// @brief Sets the Process Group ID (PGID) of the process specified by pid. +/// If pid is zero, the process ID of the calling process is used. +/// @param pid process of which we want to set the PGID. +/// @param pgid the PGID we want to set. +/// @return returns zero. On error, -1 is returned, and errno is set appropriately. +int setpgid(pid_t pid, pid_t pgid); + ///@brief returns the group ID of the calling process. ///@return GID of the current process extern pid_t getgid(); @@ -94,6 +107,16 @@ extern pid_t getgid(); /// Otherwise returns -1 with errno set to :EINVAL or EPERM extern int setgid(pid_t pid); +///@brief Returns the User ID of the calling process. +///@return User ID of the current process. +extern uid_t getuid(); + +///@brief Sets the effective User ID of the calling process. +///@param uid the new User ID. +///@return On success, zero is returned. +/// Otherwise returns -1 with errno set to :EINVAL or EPERM +extern int setuid(uid_t uid); + /// @brief Returns the parent process ID (PPID) of the calling process. /// @return pid_t parent process identifier. extern pid_t getppid(); diff --git a/libc/src/unistd/getpgid.c b/libc/src/unistd/getpgid.c new file mode 100644 index 0000000..783da97 --- /dev/null +++ b/libc/src/unistd/getpgid.c @@ -0,0 +1,11 @@ +/// MentOS, The Mentoring Operating system project +/// @file getpgid.c +/// @brief +/// @copyright (c) 2014-2021 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include "sys/unistd.h" +#include "system/syscall_types.h" +#include "sys/errno.h" + +_syscall1(pid_t, getpgid, pid_t, pid) diff --git a/libc/src/unistd/getuid.c b/libc/src/unistd/getuid.c new file mode 100644 index 0000000..9649759 --- /dev/null +++ b/libc/src/unistd/getuid.c @@ -0,0 +1,11 @@ +/// MentOS, The Mentoring Operating system project +/// @file getuid.c +/// @brief +/// @copyright (c) 2014-2021 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include "sys/unistd.h" +#include "system/syscall_types.h" +#include "sys/errno.h" + +_syscall0(uid_t, getuid) diff --git a/libc/src/unistd/setpgid.c b/libc/src/unistd/setpgid.c new file mode 100644 index 0000000..b573771 --- /dev/null +++ b/libc/src/unistd/setpgid.c @@ -0,0 +1,11 @@ +/// MentOS, The Mentoring Operating system project +/// @file setpgid.c +/// @brief +/// @copyright (c) 2014-2021 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include "sys/unistd.h" +#include "system/syscall_types.h" +#include "sys/errno.h" + +_syscall2(int, setpgid, pid_t, pid, pid_t, pgid) diff --git a/libc/src/unistd/setuid.c b/libc/src/unistd/setuid.c new file mode 100644 index 0000000..bf8e906 --- /dev/null +++ b/libc/src/unistd/setuid.c @@ -0,0 +1,11 @@ +/// MentOS, The Mentoring Operating system project +/// @file setuid.c +/// @brief +/// @copyright (c) 2014-2021 This file is distributed under the MIT License. +/// See LICENSE.md for details. + +#include "sys/unistd.h" +#include "system/syscall_types.h" +#include "sys/errno.h" + +_syscall1(int, setuid, uid_t, pid) diff --git a/mentos/inc/fs/ext2.h b/mentos/inc/fs/ext2.h index 9d1ffbd..0968c85 100644 --- a/mentos/inc/fs/ext2.h +++ b/mentos/inc/fs/ext2.h @@ -15,5 +15,3 @@ int ext2_initialize(); /// @brief De-initializes the EXT2 drivers. /// @return 0 on success, 1 on error. int ext2_finalize(); - -void ext2_test(); diff --git a/mentos/inc/process/process.h b/mentos/inc/process/process.h index 39a455d..57af125 100644 --- a/mentos/inc/process/process.h +++ b/mentos/inc/process/process.h @@ -82,9 +82,11 @@ typedef struct task_struct { pid_t pid; /// The session id of the process pid_t sid; - /// The Group Id of the process + /// The Process Group Id of the process + pid_t pgid; + /// The Group ID (GID) of the process pid_t gid; - /// The uid of the user owning the process. + /// The User ID (UID) of the user owning the process. pid_t uid; // -1 unrunnable, 0 runnable, >0 stopped. /// The current state of the process: diff --git a/mentos/inc/system/syscall.h b/mentos/inc/system/syscall.h index f52ccde..b0063b7 100644 --- a/mentos/inc/system/syscall.h +++ b/mentos/inc/system/syscall.h @@ -117,6 +117,19 @@ pid_t sys_getsid(pid_t pid); /// Otherwise return -1 with errno : EPERM pid_t sys_setsid(); +///@brief returns the Process Group ID (PGID) of the process specified by pid. +/// If pid is zero, the process ID of the calling process is used. +/// @param pid process of which we want to know the PGID. +/// @return the PGID of the specified process. +pid_t sys_getpgid(pid_t pid); + +/// @brief Sets the Process Group ID (PGID) of the process specified by pid. +/// If pid is zero, the process ID of the calling process is used. +/// @param pid process of which we want to set the PGID. +/// @param pgid the PGID we want to set. +/// @return returns zero. On error, -1 is returned, and errno is set appropriately. +int sys_setpgid(pid_t pid, pid_t pgid); + ///@brief returns the group ID of the calling process. ///@return GID of the current process pid_t sys_getgid(); @@ -124,13 +137,23 @@ pid_t sys_getgid(); ///@brief sets the effective group ID of the calling process. ///@param pid process identifier to ///@return On success, zero is returned. -/// Otherwise returns -1 with errno set to :EINVAL or EPERM +/// Otherwise returns -1 with errno set to :EINVAL or EPERM. int sys_setgid(pid_t pid); /// @brief Returns the parent process ID (PPID) of the calling process. /// @return The parent process ID. pid_t sys_getppid(); +/// @brief Returns the User ID (UID) of the calling process. +/// @return The User ID. +uid_t sys_getuid(); + +/// @brief Tries to set the User ID (UID) of the calling process. +/// @param uid the new User ID. +///@return On success, zero is returned. +/// Otherwise returns -1 with errno set to :EINVAL or EPERM. +int sys_setuid(uid_t uid); + /// @brief Adds the increment to the priority value of the task. /// @param increment The modifier to apply to the nice value. /// @return The new nice value. diff --git a/mentos/src/fs/ext2.c b/mentos/src/fs/ext2.c index 487788b..d46b6cc 100644 --- a/mentos/src/fs/ext2.c +++ b/mentos/src/fs/ext2.c @@ -1329,11 +1329,8 @@ static int ext2_allocate_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode, /// @return the amount of data we read, or negative value for an error. static ssize_t ext2_read_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode, uint32_t block_index, uint8_t *buffer) { - if (block_index >= (inode->blocks_count / fs->blocks_per_block_count)) { - pr_err("Tried to read an invalid block `%d`, but inode only has %d\n", - block_index, (inode->blocks_count / fs->blocks_per_block_count)); + if (block_index >= (inode->blocks_count / fs->blocks_per_block_count)) return -1; - } // Get the real index. uint32_t real_index = ext2_get_real_block_index(fs, inode, block_index); if (real_index == 0) @@ -1352,8 +1349,6 @@ static ssize_t ext2_read_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode, static ssize_t ext2_write_inode_block(ext2_filesystem_t *fs, ext2_inode_t *inode, uint32_t inode_index, uint32_t block_index, uint8_t *buffer) { while (block_index >= (inode->blocks_count / fs->blocks_per_block_count)) { - pr_warning("Tried to read an invalid block `%d`, but inode only has %d!\n", - block_index, (inode->blocks_count / fs->blocks_per_block_count)); ext2_allocate_inode_block(fs, inode, inode_index, (inode->blocks_count / fs->blocks_per_block_count)); ext2_write_inode(fs, inode, inode_index); } @@ -1463,11 +1458,8 @@ static ssize_t ext2_write_inode_data(ext2_filesystem_t *fs, ext2_inode_t *inode, memset(cache, 0, fs->block_size); if (start_block == end_block) { - // Read the real block. - if (ext2_read_inode_block(fs, inode, start_block, cache) == -1) { - pr_err("Failed to read the inode block `%d`\n", start_block); - //goto free_cache_return_error; - } + // Read the real block, if we fail is not a problem. + ext2_read_inode_block(fs, inode, start_block, cache); // Copy the content back to the buffer. memcpy((uint8_t *)(((uintptr_t)cache) + ((uintptr_t)offset % fs->block_size)), buffer, size_to_write); // Write the block back. @@ -1478,11 +1470,8 @@ static ssize_t ext2_write_inode_data(ext2_filesystem_t *fs, ext2_inode_t *inode, } else { uint32_t block_offset, blocks_read = 0; for (block_offset = start_block; block_offset < end_block; ++block_offset, ++blocks_read) { - // Read the real block. - if (ext2_read_inode_block(fs, inode, block_offset, cache) == -1) { - pr_err("Failed to read the inode block `%d`\n", block_offset); - //goto free_cache_return_error; - } + // Read the real block, if we fail is not a problem. + ext2_read_inode_block(fs, inode, block_offset, cache); if (block_offset == start_block) { // Copy the content back to the buffer. memcpy((uint8_t *)(((uintptr_t)cache) + ((uintptr_t)offset % fs->block_size)), buffer, fs->block_size - (offset % fs->block_size)); @@ -1497,11 +1486,8 @@ static ssize_t ext2_write_inode_data(ext2_filesystem_t *fs, ext2_inode_t *inode, } } if (end_size) { - // Read the real block. - if (ext2_read_inode_block(fs, inode, end_block, cache) == -1) { - pr_err("Failed to read the inode block `%d`\n", block_offset); - //goto free_cache_return_error; - } + // Read the real block, if we fail is not a problem. + ext2_read_inode_block(fs, inode, end_block, cache); // Copy the content back to the buffer. memcpy(cache, buffer + fs->block_size * blocks_read - (offset % fs->block_size), end_size); // Write the block back. @@ -1608,6 +1594,13 @@ void ext2_direntry_iterator_next(ext2_direntry_iterator_t *iterator) iterator->direntry = ext2_direntry_iterator_get(iterator); } +static inline bool_t ext2_directory_is_empty(ext2_filesystem_t *fs, uint8_t *cache, ext2_inode_t *inode) +{ + if (ext2_read_inode_block(fs, inode, 0U, cache) == -1) + return true; + return ((ext2_dirent_t *)cache) == NULL; +} + // ============================================================================ // Directory Entry Management Functions // ============================================================================ @@ -2043,6 +2036,8 @@ static int ext2_create_inode( pr_err("Failed to read the newly created inode.\n"); return -1; } + pr_debug("UID = %d\n", task->uid); + pr_debug("GID = %d\n", task->gid); // Set the inode mode. inode->mode = mode; // Set the user identifiers of the owners. @@ -2703,9 +2698,8 @@ static int ext2_rmdir(const char *path) goto free_cache_return_error; } // Check if the directory is empty, if it enters the loop then it means it is not empty. - for (ext2_direntry_iterator_t it = ext2_direntry_iterator_begin(fs, cache, &inode); - ext2_direntry_iterator_valid(&it); ext2_direntry_iterator_next(&it)) { - pr_err("The directory is not empty.\n"); + if (!ext2_directory_is_empty(fs, cache, &inode)) { + pr_err("The directory is not empty `%s`.\n", direntry.name); kmem_cache_free(cache); return -ENOTEMPTY; } @@ -3066,6 +3060,6 @@ void ext2_test() ext2_mkdir("/home/pippo", EXT2_S_IRWXU | EXT2_S_IRGRP | EXT2_S_IXGRP | EXT2_S_IROTH | EXT2_S_IXOTH); ext2_rmdir("/home/pippo"); - + ext2_rmdir("/home"); } \ No newline at end of file diff --git a/mentos/src/kernel.c b/mentos/src/kernel.c index 10d568d..ae59d2b 100644 --- a/mentos/src/kernel.c +++ b/mentos/src/kernel.c @@ -350,8 +350,6 @@ int kmain(boot_info_t *boot_informations) } print_ok(); - //ext2_test(); - // We have completed the booting procedure. pr_notice("Booting done, jumping into init process.\n"); // Print the welcome message. diff --git a/mentos/src/process/process.c b/mentos/src/process/process.c index b18f38d..b8eabb3 100644 --- a/mentos/src/process/process.c +++ b/mentos/src/process/process.c @@ -4,14 +4,12 @@ /// @copyright (c) 2014-2021 This file is distributed under the MIT License. /// See LICENSE.md for details. -#include "sys/kernel_levels.h" - // Include the kernel log levels. #include "sys/kernel_levels.h" // Change the header. #define __DEBUG_HEADER__ "[PROC ]" // Set the log level. -#define __DEBUG_LEVEL__ LOGLEVEL_NOTICE +#define __DEBUG_LEVEL__ LOGLEVEL_DEBUG #include "process/process.h" #include "process/scheduler.h" @@ -180,8 +178,9 @@ static inline task_struct *__alloc_task(task_struct *source, task_struct *parent memcpy(&proc->thread, &source->thread, sizeof(thread_struct_t)); // Set the statistics of the process. proc->uid = 0; - proc->sid = 0; proc->gid = 0; + proc->sid = 0; + proc->pgid = 0; proc->se.prio = DEFAULT_PRIO; proc->se.start_runtime = timer_get_ticks(); proc->se.exec_start = timer_get_ticks(); @@ -394,13 +393,15 @@ pid_t sys_fork(pt_regs *f) proc->thread.regs.eflags = proc->thread.regs.eflags | EFLAG_IF; // Copy session and group id of the parent into the child - proc->sid = current->sid; - proc->gid = current->gid; + proc->sid = current->sid; + proc->pgid = current->pgid; + proc->uid = current->uid; + proc->gid = current->gid; // Active the new process. scheduler_enqueue_task(proc); - pr_debug("Forked '%s' (pid: %d, gid: %d, sid: %d)...\n", proc->name, proc->pid, proc->gid, proc->sid); + pr_debug("Forked '%s' (pid: %d, gid: %d, sid: %d, pgid: %d)...\n", proc->name, proc->pid, proc->gid, proc->sid, proc->pgid); // Return PID of child process to parent. return proc->pid; diff --git a/mentos/src/process/scheduler.c b/mentos/src/process/scheduler.c index 1a1fae4..2f2def8 100644 --- a/mentos/src/process/scheduler.c +++ b/mentos/src/process/scheduler.c @@ -259,7 +259,7 @@ wait_queue_entry_t *sleep_on(wait_queue_head_t *wq) return wait_entry; } -int is_orphaned_pgrp(pid_t gid) +int is_orphaned_pgrp(pid_t pgid) { pid_t sid = 0; @@ -267,7 +267,7 @@ int is_orphaned_pgrp(pid_t gid) list_head *it; list_for_each (it, &runqueue.queue) { task_struct *task = list_entry(it, task_struct, run_list); - if (task->gid == gid) { + if (task->pgid == pgid) { sid = task->sid; break; } @@ -309,11 +309,10 @@ pid_t sys_getsid(pid_t pid) list_head *it; list_for_each (it, &runqueue.queue) { task_struct *task = list_entry(it, task_struct, run_list); - if (task->pid == pid) - { - if(runqueue.curr->sid != task->sid) + if (task->pid == pid) { + if (runqueue.curr->sid != task->sid) return -EPERM; - + return task->sid; } } @@ -326,54 +325,83 @@ pid_t sys_setsid() if (task == NULL) { kernel_panic("There is no current process!"); } - if (task->sid == task->pid) - { + if (task->sid == task->pid) { pr_debug("Process %d is already a session leader.", task->pid); return -EPERM; } - task->sid = task->pid; - task->gid = task->pid; + task->sid = task->pid; + task->pgid = task->pid; return task->sid; } +pid_t sys_getpgid(pid_t pid) +{ + task_struct *task = NULL; + if (pid == 0) + task = runqueue.curr; + else + task = scheduler_get_running_process(pid); + if (task) + return task->pgid; + return 0; +} + +int sys_setpgid(pid_t pid, pid_t pgid) +{ + task_struct *task = NULL; + if (pid == 0) + task = runqueue.curr; + else + task = scheduler_get_running_process(pid); + if (task) { + if (task->pgid == task->pid) + pr_debug("Process %d is already a session leader.", task->pid); + task->pgid = pgid; + } + return 0; +} + +uid_t sys_getuid() +{ + if (runqueue.curr) + return runqueue.curr->uid; + return -EPERM; +} + +int sys_setuid(uid_t uid) +{ + if (runqueue.curr && (runqueue.curr->uid == 0)) { + runqueue.curr->uid = uid; + return 0; + } + return -EPERM; +} + pid_t sys_getgid() { - task_struct *curr = runqueue.curr; - if (curr == NULL) { - kernel_panic("There is no current process!"); + if (runqueue.curr) { + return runqueue.curr->gid; } - - return curr->gid; + return -EPERM; } int sys_setgid(pid_t gid) { - task_struct *curr = runqueue.curr; - if (curr == NULL) { - kernel_panic("There is no current process!"); + if (runqueue.curr && (runqueue.curr->uid == 0)) { + runqueue.curr->gid = gid; + return 0; } - - if (curr->gid == curr->pid) - pr_debug("Process %d is already a session leader.", task->pid); - - curr->gid = curr->pid; - return 0; + return -EPERM; } pid_t sys_getppid() { // Get the current task. - if (runqueue.curr == NULL) { - kernel_panic("There is no current process!"); - } - if (runqueue.curr->parent == NULL) { - return 0; - } - - // Return the parent process identifer of the process. - return runqueue.curr->parent->pid; + if (runqueue.curr && runqueue.curr->parent) + return runqueue.curr->parent->pid; + return -EPERM; } int sys_nice(int increment) diff --git a/mentos/src/system/signal.c b/mentos/src/system/signal.c index 3cd8155..20945c2 100644 --- a/mentos/src/system/signal.c +++ b/mentos/src/system/signal.c @@ -457,7 +457,7 @@ int do_signal(struct pt_regs *f) case SIGTSTP: case SIGTTIN: case SIGTTOU: - if (is_orphaned_pgrp(current->gid)) + if (is_orphaned_pgrp(current->pgid)) continue; case SIGSTOP: diff --git a/mentos/src/system/syscall.c b/mentos/src/system/syscall.c index b8931df..e9026dd 100644 --- a/mentos/src/system/syscall.c +++ b/mentos/src/system/syscall.c @@ -63,8 +63,12 @@ void syscall_init() sys_call_table[__NR_getpid] = (SystemCall)sys_getpid; sys_call_table[__NR_getsid] = (SystemCall)sys_getsid; sys_call_table[__NR_setsid] = (SystemCall)sys_setsid; - sys_call_table[__NR_getgid] =(SystemCall)sys_getgid; - sys_call_table[__NR_setgid] =(SystemCall)sys_setgid; + sys_call_table[__NR_getpgid] = (SystemCall)sys_getpgid; + sys_call_table[__NR_setpgid] = (SystemCall)sys_setpgid; + sys_call_table[__NR_getuid] = (SystemCall)sys_getuid; + sys_call_table[__NR_setuid] = (SystemCall)sys_setuid; + sys_call_table[__NR_getgid] = (SystemCall)sys_getgid; + sys_call_table[__NR_setgid] = (SystemCall)sys_setgid; sys_call_table[__NR_getppid] = (SystemCall)sys_getppid; sys_call_table[__NR_sigaction] = (SystemCall)sys_sigaction; sys_call_table[__NR_fork] = (SystemCall)sys_fork; diff --git a/programs/login.c b/programs/login.c index 209afea..5e8b7f5 100644 --- a/programs/login.c +++ b/programs/login.c @@ -183,6 +183,9 @@ int main(int argc, char **argv) return 1; } + setgid(pwd->pw_gid); + setuid(pwd->pw_uid); + char *_argv[] = { pwd->pw_shell, (char *)NULL }; if (execv(pwd->pw_shell, _argv) == -1) { printf("%s: Failed to execute the shell.\n", argv[0]); diff --git a/programs/mkdir.c b/programs/mkdir.c index 5f74eb3..6008a0e 100644 --- a/programs/mkdir.c +++ b/programs/mkdir.c @@ -5,6 +5,7 @@ /// See LICENSE.md for details. #include +#include #include #include #include @@ -23,7 +24,7 @@ int main(int argc, char *argv[]) printf(" %s \n", argv[0]); return 0; } - if (mkdir(argv[1], 0)) { + if (mkdir(argv[1], S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) { printf("%s: cannot create directory '%s': %s\n", argv[0], argv[1], strerror(errno)); } return 0; diff --git a/programs/shell.c b/programs/shell.c index 269d6d2..5812244 100644 --- a/programs/shell.c +++ b/programs/shell.c @@ -747,7 +747,7 @@ int main(int argc, char *argv[]) if (cpid == 0) { // Makes the new process a group leader pid_t pid = getpid(); - setgid(pid); + setpgid(cpid, pid); if (execvp(_argv[0], _argv) == -1) { printf("\nUnknown command: %s\n", _argv[0]); diff --git a/programs/touch.c b/programs/touch.c index 8f56bb5..379ecc4 100644 --- a/programs/touch.c +++ b/programs/touch.c @@ -27,7 +27,7 @@ int main(int argc, char** argv) } int fd = open(argv[1], O_RDONLY, 0); if (fd < 0) { - fd = open(argv[1], O_CREAT, 0); + fd = open(argv[1], O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); if (fd >= 0) { close(fd); }