Framing the Implementation
My current thinking revolves around firmly establishing the goal: a Unix-like permission model for user, group, and others. I'm focusing on the core principles and how they translate into a functional structure. I'm prioritizing clarity and modularity in the design.
Mapping Permissions to Code
I'm now diving into the granular mechanics of implementing this within a custom operating system. The challenge is in moving from the abstract Unix permission concept into concrete data structures and kernel-level logic, not merely using a pre-existing chmod function. We need to create a data model for users, groups, and inodes. We need to define how the kernel would parse permission bits. It's about designing the checks that the OS will do.
Refining Data Models
I'm presently building upon the data structures I've sketched. The process control block will house user and group IDs. Inodes, or file nodes, are designed to store file ownership (UID, GID) and access modes. I'm focusing on defining system calls like chmod and chown, and the essential access check. Now I'm preparing a foundation for bitmask types and essential constants. I'm focusing on the structural details of each piece.
Outlining Permission Checks
I'm presently building out the central permission checking function. The initial structure is in place: root overrides, UID/GID matching, and others. I'm focusing on the branching logic and interactions between the checks, based on the Unix model. I've sketched a base check but it needs refinement to exclude merging permission modes. It's about designing how the kernel would parse permission bits.
Structuring the Code
I'm now focusing on how the permission logic fits into the virtual file system. vfs_open() will call a check_permission function, and sys_execve() will also need it, albeit with a different access mode. I'm exploring bitwise operations for permissions, implementing EACCES for errors, and defining the root user exception. I'm defining octal constants, structs for process and file representation, and planning the vfs_permission function.
Expanding the Scope
I'm now integrating the nuanced considerations that arose, focusing on file creation with default permissions via umask, setuid/setgid for privilege elevation, and the sticky bit for directory protection. My next step is organizing the detailed documentation for this implementation.
Formalizing Access Logic
I'm presently building out the final layers. I'm focusing on defining how the user, group, and other relationships with the file objects will interact, formalizing the core "Subject -> Object" connection. I'm also preparing for final cleaning and ensuring C-style adherence throughout the codebase. The next steps will refine the overall documentation.
Implementing a Unix-like permission model requires a Discretionary Access Control (DAC) system. This involves defining Subjects (Processes acting on behalf of users), Objects (Files/Resources), and Access Rights (Read, Write, Execute).
Here is a step-by-step implementation plan for a custom Operating System kernel (written in C-style pseudocode).
1. Define Primitive Types and Constants
First, we need the basic data types for identities and the bitmasks that represent permissions.
#include <stdint.h>
#include <stdbool.h>
// Type Definitions
typedef uint32_t uid_t; // User ID
typedef uint32_t gid_t; // Group ID
typedef uint16_t mode_t; // Bitmask for file mode/permissions
// Special IDs
#define ROOT_UID 0
#define ROOT_GID 0
// Permission Bitmasks (Octal standard)
// Owner (User)
#define S_IRUSR 00400 // Read
#define S_IWUSR 00200 // Write
#define S_IXUSR 00100 // Execute
// Group
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010
// Others (World)
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001
// Operation Flags (for checking access)
#define MAY_READ 4
#define MAY_WRITE 2
#define MAY_EXEC 1
2. Data Structures (Subject and Object)
We need to modify the Process Control Block (PCB) and the File Inode structure to store credentials.
The Subject (Process)
The process structure must hold the identity of the user running it.
typedef struct {
// Static IDs (Real ID - who started the process)
uid_t uid;
gid_t gid;
// Effective IDs (Used for permission checks, enables setuid)
uid_t euid;
gid_t egid;
// Supplementary Groups (Standard Unix supports multiple groups)
gid_t groups[32];
int group_count;
// File creation mask
mode_t umask;
} task_struct;
// Global pointer to the currently running process
extern task_struct* current_process;
The Object (Inode/File)
The file system node must store ownership and the permission bitmask.
typedef struct {
uint32_t inode_id;
// Permissions
uid_t uid; // Owner
gid_t gid; // Group
mode_t mode; // The 16-bit permission mask (e.g., 0755)
// ... other file data (size, pointers, etc.)
} inode_t;
3. The Core Permission Algorithm
This is the logic kernel that runs whenever a process tries to touch a file. The standard Unix rule is: Match specific to general.
- If Owner, use Owner bits (ignore Group/Other).
- Else if Group, use Group bits (ignore Other).
- Else use Other bits.
// Function returns 0 on success, -EACCES on failure
int vfs_kcheck_permission(inode_t* inode, int mask) {
task_struct* task = current_process;
mode_t mode = inode->mode;
// 1. Root Bypass
// Root (UID 0) can usually Read/Write anything.
// For Execute, root generally needs at least one 'x' bit set in the file.
if (task->euid == ROOT_UID) {
if (mask & MAY_EXEC) {
// If looking for exec, at least one execute bit must be on
// (unless it's a directory, which root can always traverse)
if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) return 0;
return -1; // Permission denied (file not executable)
}
return 0; // Allow Read/Write
}
// 2. Check Owner
if (task->euid == inode->uid) {
// Shift mask to align with User bits (reading bit 4, 2, or 1)
mode_t req = (mask << 6);
if ((mode & req) == req) return 0; // Granted
return -1; // Denied (Owner explicitly restricted)
}
// 3. Check Group
// We check the effective GID and the supplementary group list
bool in_group = (task->egid == inode->gid);
if (!in_group) {
for (int i = 0; i < task->group_count; i++) {
if (task->groups[i] == inode->gid) {
in_group = true;
break;
}
}
}
if (in_group) {
mode_t req = (mask << 3); // Shift to Group bits
if ((mode & req) == req) return 0;
return -1;
}
// 4. Check Others
if ((mode & mask) == mask) return 0;
// 5. Deny
return -1;
}
4. Integration into System Calls
You must inject this check into your Virtual File System (VFS) operations.
Opening a File
When open() is called, determine the intent (Read vs Write) and check.
int sys_open(const char* path, int flags) {
inode_t* inode = vfs_lookup(path); // Find the file
if (!inode) return -PF_NOTFOUND;
int acc_mode = 0;
if (flags & O_RDWR) acc_mode = MAY_READ | MAY_WRITE;
else if (flags & O_WRONLY) acc_mode = MAY_WRITE;
else acc_mode = MAY_READ;
// Enforce Permissions
if (vfs_kcheck_permission(inode, acc_mode) != 0) {
return -EACCES; // "Permission Denied"
}
return create_file_handle(inode);
}
Creating a File (umask)
When creating a file, the process umask is applied. umask subtracts permissions.
int sys_creat(const char* path, mode_t requested_mode) {
task_struct* task = current_process;
// Logic: mode = requested & ~umask
// e.g., requested 0666 & ~0022 = 0644
mode_t final_mode = requested_mode & ~(task->umask);
inode_t* new_inode = vfs_create_node(path);
// Set ownership to current process
new_inode->uid = task->euid;
new_inode->gid = task->egid;
new_inode->mode = final_mode;
return 0;
}
5. Changing Permissions (chmod/chown)
You need syscalls to modify these bits. Only the Owner (or Root) can change permissions or ownership.
int sys_chmod(const char* path, mode_t new_mode) {
inode_t* inode = vfs_lookup(path);
if (!inode) return -ENOENT;
task_struct* task = current_process;
// Only Root or Owner can chmod
if (task->euid != ROOT_UID && task->euid != inode->uid) {
return -EPERM; // "Operation not permitted"
}
// In real OS, sanitize new_mode (remove file type bits)
inode->mode = (inode->mode & 0xF000) | (new_mode & 0x0FFF);
return 0;
}
int sys_chown(const char* path, uid_t new_uid, gid_t new_gid) {
inode_t* inode = vfs_lookup(path);
task_struct* task = current_process;
// Generally only Root can change file ownership to avoid
// users hiding quotas or creating security holes.
if (task->euid != ROOT_UID) {
return -EPERM;
}
inode->uid = new_uid;
inode->gid = new_gid;
return 0;
}
6. Advanced: SetUID / SetGID
A critical part of Unix is privilege escalation via setuid bits (e.g., how passwd or sudo works). This happens in the exec syscall.
- Constants:
#define S_ISUID 04000 // Set user ID on execution
#define S_ISGID 02000 // Set group ID on execution
- Implementation in
sys_exec:
int sys_execve(const char* path, char** argv, char** envp) {
inode_t* inode = vfs_lookup(path);
// Check Execute Permission
if (vfs_kcheck_permission(inode, MAY_EXEC) != 0) return -EACCES;
// Load binary...
// Apply SetUID
if (inode->mode & S_ISUID) {
current_process->euid = inode->uid; // Elevate Effective UID
}
// Apply SetGID
if (inode->mode & S_ISGID) {
current_process->egid = inode->gid; // Elevate Effective GID
}
// Start execution...
return 0;
}
Summary of Flow
- User logs in -> Process gets UID/GID.
- Process calls
open("file.txt", O_WRONLY).
- VFS looks up inode.
- Permission Check:
- Are you Root? -> Yes.
- Are you Owner? -> Check owner write bit.
- Are you Group? -> Check group write bit.
- Are you Other? -> Check other write bit.
- If check fails -> Return error
-EACCES.
- If check passes -> Return file descriptor.