Implement a Unix-like permission model in a custom operating system.
- User
Implement a Unix-like permission model in a custom operating system.
- Assistant
Evaluating OS Enforcement Points
I'm considering enforcement points like file I/O, IPC, signals, and thinking about how the "Unix-like permission model" might specifically refer to file permissions. At the OS level, there are file objects, devices, pipes, and also aspects like process visibility and signal handling. I realize that UIDs and GIDs apply to all kernel objects. My answer needs to be manageable, aiming for 1000-1500 words with some pseudo code included, balancing depth without becoming overwhelming.Structuring Permission Model Steps
I'm planning to outline my answer in a structured way. First, I'll start with an overview. Then, I’ll define identities like user and group representation, outline permission bits and constants, and extend the VFS inode structure with uid/gid/mode. After that, I’ll track credentials in the process/task structure and implement a permission check routine with pseudo-code. I'll also apply these checks in syscalls and include system calls for modifying permissions, and maybe add advanced flags like setuid. Finally, I’ll create a minimal user database and a login path, opting for a C-style since OS kernels generally use it.Explaining Permission Bits Layout
I'm outlining the permission bits layout, considering a 16-bit or 12-bit configuration. The breakdown is as follows: bits 0-2 for others (read, write, execute), bits 3-5 for the group, bits 6-8 for the user, and bits 9-11 for special bits like setuid, setgid, and sticky. For file types, like in Unix's
S_IFMTmask, I can mention it but don't need to go into details. I plan to define constants for permissions and describe an algorithm for checking permissions, noting that root has unique permissions.Implementing Permission Check LogicI'm thinking about how to implement a permission check function. If the effective user ID (euid) is root, the function will bypass checks and return true. For other users, I’ll compare their credentials to the inode's uid and gid. Based on those checks, I'll determine the permission bits.
For directories, activities like listing, creating, or deleting require specific permissions. I also need to maintain the umask when applying modes. Lastly, I’ll mention the setuid and setgid semantics, noting how these affect the effective user and group IDs during execution.Addressing Setuid and Directory Permissions
I need to address how setuid root programs work, where the effective user ID (euid) becomes 0 while the real user ID remains unchanged, allowing for privilege drops. When discussing directories, I’ll note that in Linux, the setgid bit ensures new files inherit the directory's group, applying to new subdirectories too.
The sticky bit is important for directories, as it limits unlinking and renaming to the file owner, directory owner, or root. For user databases, I can briefly mention a static table in the kernel or a file in the file system accessible by the login process.
I'll also touch on storing user IDs for setuid transitions and provide pseudo-code for the execve function to show how the euid is modified. Signals are another aspect to denote, specifically the permissions for sending them based on user IDs. I don’t need to cover concurrency or locking in detail, so I’m ready to begin the answer. Below is a concrete blueprint for adding a Unix-like permission model to a custom OS, assuming a C-like kernel and a basic filesystem/VFS already exist.
1. Core concepts to support
Unix-style permissions require:
-
Identities
- Users: identified by UID (integer).
- Groups: identified by GID (integer).
- A special user
root(UID 0).
-
Permissions
- Access bits: read, write, execute.
- Scoped for: owner (user), group, others.
- Extra bits: setuid, setgid, sticky.
-
Objects
- At minimum: files and directories.
- Optionally: devices, IPC objects, etc.
-
Credentials
- Each process carries a credential set: real/effective UIDs & GIDs, supplementary groups, umask.
-
System calls
open,mkdir,unlink,chmod,chown,umask,execvemust enforce and change permissions.
2. Representing identities
2.1 UID / GID types
typedef uint32_t uid_t; typedef uint32_t gid_t; #define UID_ROOT 0You can adjust to 16-bit if you want a very small system, but 32-bit is future-proof.
2.2 User & group database (in userspace or simple kernel service)
Minimal approach:
- Store users in a simple file (e.g.
/etc/passwd), parsed by a userlandloginprogram. - Kernel only sees numeric
uid_t/gid_tfromloginvia a system call likesetuid().
If you want the kernel to know accounts:
struct user_record { uid_t uid; gid_t primary_gid; // optional: username, home dir, etc. }; struct group_record { gid_t gid; // optional: group name };But strictly speaking, the DAC mechanism itself needs only the numeric UIDs/GIDs.
3. Permission bits and file mode
3.1 Mode layout
Use the standard Unix-like 16-bit mode:
- Bits 0–2: other permissions (r=4, w=2, x=1)
- Bits 3–5: group permissions
- Bits 6–8: owner permissions
- Bits 9–11: special bits: setuid, setgid, sticky
- Higher bits: file type (regular, dir, symlink, …)
Define flags:
typedef uint16_t mode_t; // User bits #define S_IRUSR 0400 // owner read #define S_IWUSR 0200 // owner write #define S_IXUSR 0100 // owner execute // Group bits #define S_IRGRP 0040 #define S_IWGRP 0020 #define S_IXGRP 0010 // Other bits #define S_IROTH 0004 #define S_IWOTH 0002 #define S_IXOTH 0001 // Special bits #define S_ISUID 04000 #define S_ISGID 02000 #define S_ISVTX 01000 // sticky bit // File type masks (optional, but typical) #define S_IFMT 0170000 // type mask #define S_IFREG 0100000 // regular file #define S_IFDIR 0040000 // directory // etc. #define S_PERM_MASK 07777 // all perms + special bits
4. Attach permissions to filesystem objects
Extend your inode (or equivalent VFS node):
struct inode { mode_t mode; // includes perms + type bits uid_t uid; // owner gid_t gid; // group // ... existing fields (size, timestamps, block pointers, etc.) };The on-disk format of your filesystem must store
uid,gidandmodein each inode, and your VFS layer must keep them in memory.
5. Process credentials
Each process must carry credentials:
#define MAX_GROUPS 16 // or more, as needed struct cred { uid_t ruid; // real UID uid_t euid; // effective UID uid_t suid; // saved set-user-ID (for setuid exec) gid_t rgid; // real GID gid_t egid; // effective GID gid_t sgid; // saved set-group-ID gid_t groups[MAX_GROUPS]; // supplementary groups int ngroups; mode_t umask; // process umask }; struct task { // ... struct cred cred; // ... };- Real UID/GID: "owner" of the process (typically from login).
- Effective UID/GID: used for permission checks.
- Saved: used by setuid/setgid binaries to drop/regain privileges.
6. Permission check routine
Centralize permission checking in one function used by all relevant syscalls.
6.1 Helper: group membership
bool in_group(const struct cred *c, gid_t gid) { if (c->egid == gid || c->rgid == gid) return true; for (int i = 0; i < c->ngroups; i++) if (c->groups[i] == gid) return true; return false; }6.2 Check access to an inode
Define an access mask, similar to POSIX
R_OK,W_OK,X_OK:#define MAY_READ 0x4 #define MAY_WRITE 0x2 #define MAY_EXEC 0x1The core function:
bool inode_permission(const struct inode *ino, const struct cred *cred, int want) { // 1. root shortcut (simple variant) if (cred->euid == UID_ROOT) { // For simplicity: root can do anything. // If you want to be stricter, you can require execute bits, etc. return true; } mode_t m = ino->mode; int perm_bits; // 2. Decide which permission class applies: owner / group / others if (cred->euid == ino->uid) { perm_bits = (m >> 6) & 0x7; // owner bits } else if (in_group(cred, ino->gid)) { perm_bits = (m >> 3) & 0x7; // group bits } else { perm_bits = m & 0x7; // other bits } // 3. Check each requested access type if ((want & MAY_READ) && !(perm_bits & 0x4)) return false; if ((want & MAY_WRITE) && !(perm_bits & 0x2)) return false; if ((want & MAY_EXEC) && !(perm_bits & 0x1)) return false; return true; }You'll call
inode_permission()in path lookup and syscalls.
7. Directory-specific semantics
Directory permissions are interpreted differently:
- Read (r): can list entries (
readdir). - Write (w): can create and remove entries in the directory.
- Execute (x): can traverse into the directory / access by name.
Typical rules:
- To traverse each path component: need
xon each directory in the path. - To create/remove files in dir: need
wandxon that dir. - To list the contents: need
randxon that dir.
Example enforcement:
// When looking up /a/b/c: for each directory component d in { "/", "a", "b" }: if (!inode_permission(d_inode, current_cred, MAY_EXEC)) return -EACCES;For create inside directory
D:if (!inode_permission(D, current_cred, MAY_WRITE | MAY_EXEC)) return -EACCES;For
readdir(D):if (!inode_permission(D, current_cred, MAY_READ | MAY_EXEC)) return -EACCES;
8. Applying permissions in syscalls
Hook the permission check into all file-related syscalls.
8.1
open(path, flags, mode)- Resolve
pathusing directory traversal rules above. - If file exists:
- For read:
inode_permission(ino, cred, MAY_READ) - For write:
inode_permission(ino, cred, MAY_WRITE) - For execute (if
O_EXECor similar):inode_permission(ino, cred, MAY_EXEC)
- For read:
- If file doesn’t exist, and
O_CREAT:- Check parent directory permission: need
MAY_WRITE | MAY_EXECon parent. - Compute file mode:
new_mode = (mode & 0777) & ~cred->umask. - Set file’s
uid = cred->euid,gid = cred->egid(or inherit parent’s gid if you want setgid-dir behavior).
- Check parent directory permission: need
8.2
mkdir,rmdir,unlink,rename-
mkdir(parent, name, mode)- Need
MAY_WRITE | MAY_EXEConparent. - New dir’s mode:
(mode & 0777) & ~cred->umask.
- Need
-
unlink(parent, name)- Need
MAY_WRITE | MAY_EXEConparent. - Later, add sticky-bit rules (see below).
- Need
-
rename(old_parent, new_parent, ...)- Need
MAY_WRITE | MAY_EXECon both directories.
- Need
8.3
chmod(path, mode)- Resolve
path. - Only allowed if:
- caller is
root, or - caller’s
euid == inode->uid.
- caller is
- Update
inode->mode(preserve file type bits, maybe special bits as allowed).
int sys_chmod(const char *path, mode_t new_mode) { struct inode *ino = lookup(path); if (!ino) return -ENOENT; if (current_cred->euid != UID_ROOT && current_cred->euid != ino->uid) return -EPERM; ino->mode = (ino->mode & S_IFMT) | (new_mode & S_PERM_MASK); mark_inode_dirty(ino); return 0; }8.4
chown(path, new_uid, new_gid)- Typically only
rootcan change owner. - Some systems let owner change group if they’re a member; you can simplify to root-only.
int sys_chown(const char *path, uid_t uid, gid_t gid) { struct inode *ino = lookup(path); if (!ino) return -ENOENT; if (current_cred->euid != UID_ROOT) return -EPERM; if (uid != (uid_t)-1) ino->uid = uid; if (gid != (gid_t)-1) ino->gid = gid; mark_inode_dirty(ino); return 0; }8.5
umask(mask)Per-process mask applied when creating new files/dirs:
mode_t sys_umask(mode_t new_mask) { mode_t old = current->cred.umask; current->cred.umask = new_mask & 0777; return old; }Use when creating:
mode_t mode = (requested_mode & 0777) & ~current->cred.umask;
9. Setuid, setgid, sticky bits
9.1 Setuid and setgid on regular files
On
execve()of fileF:int sys_execve(const char *path, ... ) { struct inode *ino = lookup(path); if (!ino) return -ENOENT; // Check execute permission first if (!inode_permission(ino, ¤t->cred, MAY_EXEC)) return -EACCES; // Setuid/setgid behavior uid_t new_euid = current->cred.euid; gid_t new_egid = current->cred.egid; if (ino->mode & S_ISUID) new_euid = ino->uid; if (ino->mode & S_ISGID) new_egid = ino->gid; // Load binary, replace address space, etc... current->cred.suid = new_euid; current->cred.euid = new_euid; current->cred.sgid = new_egid; current->cred.egid = new_egid; // run new program }This allows programs owned by root (and setuid) to run with elevated privileges.
9.2 Setgid on directories
Common semantics (Linux-style):
- New files inside a setgid directory get directory’s group.
- New subdirectories inherit the setgid bit as well.
On create in directory
D:gid_t new_gid = current->cred.egid; mode_t new_mode = (mode & 0777) & ~current->cred.umask; if (D->mode & S_ISGID) { new_gid = D->gid; new_mode |= S_ISGID; // if creating subdirectory }9.3 Sticky bit on directories
On a sticky directory (e.g.
/tmp), users can only delete/rename files they own:- If
D->mode & S_ISVTX, then forunlink(D, child):- Allowed if:
- caller is
root, OR - caller’s
euid == D->uid(dir owner), OR - caller’s
euid == child->uid(file owner).
- caller is
- Allowed if:
Example:
bool can_unlink(struct inode *dir, struct inode *child, const struct cred *cred) { if (!inode_permission(dir, cred, MAY_WRITE | MAY_EXEC)) return false; if (!(dir->mode & S_ISVTX)) return true; if (cred->euid == UID_ROOT) return true; if (cred->euid == dir->uid) return true; if (cred->euid == child->uid) return true; return false; }
10. Minimal login/auth integration
Simple flow:
- Boot to a getty-like program on console.
loginasks for username/password; checks against/etc/passwd//etc/shadow.- On success:
logincallssetuid(user_uid),setgid(user_gid),setgroups(...),set_umask(...).- Then
execve()user’s shell.
Kernel system calls to support that:
int sys_setuid(uid_t uid) { // Usually only root can arbitrarily change UID. if (current->cred.euid != UID_ROOT) return -EPERM; current->cred.ruid = uid; current->cred.euid = uid; current->cred.suid = uid; return 0; } int sys_setgid(gid_t gid) { /* similar */ }Or you implement full POSIX semantics for non-root
setuid, but that’s optional at first.
11. Extending beyond the filesystem (optional)
Once you have UIDs/GIDs and this check function, you can apply the same model to:
- Signals: only allow sending signals if sender’s UID matches target’s real/effective UID, or sender is root.
- Process inspection (e.g. /proc): restrict reading other processes’ state.
- IPC (message queues, shared memory): attach
uid/gid/modeto each object and re-useinode_permission()logic.
If you tell me:
- what language your kernel is in,
- what your existing VFS/inode/task structures look like,
I can adapt this into more concrete, drop-in code for your environment.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.