Added syscalls for utmp management:
- SYSCALL_UTMP_LOGIN (145): register login session
- SYSCALL_UTMP_LOGOUT (146): register logout
- SYSCALL_UTMP_DEAD (147): register process death
Updated kernel syscall.c to handle utmp syscalls.
Updated kernel include/syscall.h with new syscall numbers.
Updated userspace include/syscall.h with new syscall numbers.
Updated userspace include/utmp.h with utmp_login/logout/dead declarations.
Updated userspace src/utmp.c with syscall wrappers.
Updated login command to call utmp_login() with TTY name and hostname.
Tulio A M Mendes [Thu, 11 Jun 2026 02:53:47 +0000 (23:53 -0300)]
uid: add /etc/shadow and check_password function
Implement UID Infrastructure: autenticação real (completed).
Added /etc/shadow file with basic entries (root:root, daemon:*, nobody:*).
Updated Makefile to include /etc/shadow in initrd.
Added check_password() function in pwd_grp.c to verify passwords against /etc/shadow.
Added check_password() declaration in pwd.h.
Password verification uses plaintext comparison for now (TODO: add SHA256/crypt).
Locked accounts (passwd starts with '*' or '!') are rejected.
Added /etc/passwd file with basic entries (root, daemon, nobody).
Updated Makefile to include /etc/passwd in initrd.
Existing pwd_grp.c already implements getpwnam/getpwuid/setpwent/endpwent/getpwent
with /etc/passwd parsing and static fallback.
Added utmp.h with Linux-compatible utmp structure and record types.
Implemented utmp.c with:
- utmp_init(): Initialize utmp database
- utmp_login(): Register user login session
- utmp_logout(): Mark session as DEAD_PROCESS
- utmp_dead(): Record process exit status
- utmp_get_by_pid(): Find utmp entry by PID
Reduced UTMP_MAX_ENTRIES to 32 to avoid BSS overflow.
Not initialized in boot (deferred to when needed) to prevent SMP panic.
Test: make test-battery PASS (157/157), make analyzer PASS
Note: Userspace integration (getlogin/who) and login session
registration still needed for complete L2 implementation.
Added is_root flag to overlay_node to distinguish root from wrapper nodes.
Modified overlay_root_close() to properly manage root node refcount and
free overlayfs structure when root is finally closed.
Initialized root node refcount to 1 and set is_root flag in overlayfs_create_root().
This ensures proper cleanup of overlayfs structures when the filesystem
is unmounted, preventing memory leaks of the overlayfs structure and root node.
Test: make test-battery PASS (157/157), make analyzer PASS
Tulio A M Mendes [Thu, 11 Jun 2026 02:12:56 +0000 (23:12 -0300)]
vfs: add ENAMETOOLONG checks to path helpers in fs.c
Implement L1: standardize ENAMETOOLONG across all path helpers.
Added path length checks (PATH_MAX = 128) to:
- vfs_lookup_depth(): Check path length before lookup
- vfs_lookup_parent(): Check path length before parent lookup
- vfs_mkdirp(): Check path length before recursive mkdir
These functions now return NULL or -ENAMETOOLONG when paths exceed
128 characters, matching the behavior already present in syscall.c
helpers (copy_user_cstr, path_resolve_user).
Test: make test-battery PASS (157/157), make analyzer PASS
Tulio A M Mendes [Thu, 11 Jun 2026 02:11:18 +0000 (23:11 -0300)]
shm: implement SHM_RDONLY flag for read-only attach
Implement H3: SHM permissions complete with SHM_RDONLY attach.
Added SHM_RDONLY flag (0x1000) to shm.h for shmat syscall.
Modified shm_at() to accept shmflg parameter and:
- Check write permission only if SHM_RDONLY is not set
- Map pages without VMM_FLAG_RW when SHM_RDONLY is set
- This allows read-only shared memory segments for POSIX compliance
Updated syscall handler to pass shmflg from sc_arg2 to shm_at().
Test: make test-battery PASS (157/157), make analyzer PASS
Tulio A M Mendes [Thu, 11 Jun 2026 02:09:53 +0000 (23:09 -0300)]
vfs: add vfs_check_permission_real for POSIX strict access()
Implement M2: POSIX access() with real IDs instead of effective IDs.
Added vfs_check_permission_real() function that uses current_process->uid
and current_process->gid instead of euid/egid for POSIX strict compliance.
The access() syscall now uses this function, while other operations continue
to use vfs_check_permission() with effective IDs.
This ensures access() behaves according to POSIX specification which requires
real IDs for permission checks, while preserving existing behavior for other
file operations that use effective IDs.
Test: make test-battery PASS (157/157), make analyzer PASS
Tulio A M Mendes [Thu, 11 Jun 2026 01:45:54 +0000 (22:45 -0300)]
kva_alloc: document linear mapping overlap with boot.S
Add documentation note explaining that the KVA allocator region
(0xC0500000..0xC0800000) falls within the initial 16MB linear mapping
set up by boot.S (0xC0000000..0xC0100000). This means V2P() works by
coincidence for addresses in this range, but code should use
vmm_virt_to_phys() instead to be portable and future-proof.
This documents the architectural fragility identified during H6
regression analysis.
Test: make test-battery PASS (157/157), make analyzer PASS
Tulio A M Mendes [Thu, 11 Jun 2026 01:45:05 +0000 (22:45 -0300)]
virtio-blk: fix memory leak on vring page allocation failure
Bug: If pmm_alloc_page() failed in the middle of the vring page
allocation loop, previously allocated physical pages and the VA range
were not freed, causing a memory leak.
Fix: Add rollback loop on allocation failure to:
1. Unmap and free previously allocated physical pages
2. Free the VA range via kva_free_pages()
This is a pre-existing bug that was exposed during H6 regression
analysis, not a regression introduced by H6.
Test: make test-battery PASS (157/157), make analyzer PASS
Tulio A M Mendes [Thu, 11 Jun 2026 01:43:33 +0000 (22:43 -0300)]
virtio-blk: fix V2P usage for kva_alloc addresses (H6 regression)
Bug: virtio_blk_init used V2P() macro to convert vring virtual address
to physical address. After H6, vring_va comes from kva_alloc_pages()
which may not be in the linear mapping region. V2P assumes linear
mapping (VA = PA + 0xC0000000), which is fragile.
Fix: Replace V2P(vring_va) with vmm_virt_to_phys(vring_va) to use
the proper VMM translation function that works for any mapped virtual
address.
This fixes the regression introduced by H6 where virtio-blk relied on
coincidental overlap between kva_alloc range (0xC0500000..0xC0800000)
and the initial 16MB linear mapping (0xC0000000..0xC0100000).
Test: make test-battery PASS (157/157), make analyzer PASS
Tulio A M Mendes [Thu, 11 Jun 2026 01:19:23 +0000 (22:19 -0300)]
kva: implement dynamic VA allocator to remove fixed VAs (H6)
Created kernel virtual address allocator to replace fixed VAs:
- include/kva_alloc.h: KVA allocator interface
- src/kernel/kva_alloc.c: Bitmap-based allocator with spinlock protection
- Region: 0xC0500000 .. 0xC0800000 (3 MB, 768 pages)
- kva_alloc_init(): Initialize bitmap and spinlock
- kva_alloc_pages(): Allocate contiguous page range
- kva_free_pages(): Free allocated range
Migrated fixed VA usages to dynamic allocation:
- src/hal/x86/mm.c: hal_mm_map_physical_range() now uses kva_alloc_pages
instead of fixed KVA_PHYS_MAP (0xDC000000U)
- src/drivers/virtio_blk.c: virtio vring allocation now uses kva_alloc_pages
instead of fixed VIRTIO_VRING_VA (0xC0340000U)
Removed obsolete constants:
- include/arch/x86/kernel_va_map.h: Removed KVA_PHYS_MAP constant
Updated layout comment to document KVA allocator region
Boot integration:
- src/kernel/main.c: Added kva_alloc_init() call after kheap_init()
Validation:
- make -j$(nproc): PASS
- make test SMOKE_SMP=4: 131/131 PASS
- make test-battery: 157/157 PASS
This completes H6: remove fixed VAs, replacing hardcoded virtual
addresses with dynamic allocation to prevent collisions and improve
architectural flexibility.
Tulio A M Mendes [Thu, 11 Jun 2026 01:04:02 +0000 (22:04 -0300)]
tests: add security hardening tests for H2 and M8
- Add I7: /proc/dmesg root-only access test (H2)
- Add I8: /proc/cmdline root-only access test (H2)
- Add I9: /proc/PID/maps format verification test (H2)
- Add I10: /dev/random CSPRNG uniqueness test (M8)
- Update smoke_test.exp with new test patterns
- Update test_battery.exp with new test patterns
Validation:
- make test SMOKE_SMP=4: 131/131 PASS (was 127/127, +4 new tests)
- make test-battery: 157/157 PASS (was 153/153, +4 new tests)
Tests validate H2 (/proc hardening) and M8 (CSPRNG) implementations.
Tulio A M Mendes [Thu, 11 Jun 2026 00:56:29 +0000 (21:56 -0300)]
security: implement central CSPRNG with real entropy (M8)
- Create src/kernel/csprng.c with ChaCha20-based DRBG
- Entropy sources: RDTSC, timer ticks, interrupt timing, user input
- Add csprng_init() called at boot in kernel_main()
- Add csprng_get_bytes(), csprng_get_u32(), csprng_get_u64() APIs
- Add csprng_add_entropy() for /dev/random writes
- Update src/kernel/devfs.c:
- Remove local PRNG (prng_state, prng_next)
- Use csprng_get_bytes() in dev_random_read()
- Use csprng_add_entropy() in dev_random_write()
- Spinlock protection for SMP safety
- Reseed mechanism every 256 entropy additions
Validation:
- make -j12: PASS
- make test-host: PASS (111/111)
- make test SMOKE_SMP=4: PASS (127/127)
- make test-battery: PASS (153/153)
- make analyzer: PASS
Addresses M8 from docs/URGENT_SECURITY_STATUS_2026-06-09.md
Tulio A M Mendes [Thu, 11 Jun 2026 00:52:51 +0000 (21:52 -0300)]
security: add SMP spinlock protection to futex table (H5)
- Add g_futex_lock spinlock to protect global futex_waiters table
- Protect futex_cleanup_process with spinlock
- Protect FUTEX_WAIT path with spinlock:
- Slot allocation and registration
- Timeout cleanup on early return
- Cleanup after schedule() wakeup
- Protect FUTEX_WAKE path with spinlock:
- Wakeup loop over waiters
- Existing shared keying by (addr_space, uaddr) preserved
- Prevents race conditions in SMP environments
Validation:
- make -j12: PASS
- make test-host: PASS (111/111)
- make test SMOKE_SMP=4: PASS (127/127)
- make test-battery: PASS (153/153)
- make analyzer: PASS
Addresses H5 from docs/URGENT_SECURITY_STATUS_2026-06-09.md
Tulio A M Mendes [Thu, 11 Jun 2026 00:50:48 +0000 (21:50 -0300)]
security: harden /proc with access controls and hidepid (H2)
- Protect /proc/dmesg: root-only access (euid == 0)
- Protect /proc/cmdline: root-only access (euid == 0)
- Implement hidepid mechanism with g_proc_hidepid global:
- 0 = visible to all (default)
- 1 = invisible to non-root, except own process (current default)
- 2 = invisible to non-root, including own process
- Redact addresses in /proc/<pid>/maps for non-root users:
- Show 0 instead of actual heap/brk/mmap addresses
- Applies even to own process for security
- Update proc_root_readdir to respect hidepid for PID listing
- Update proc_root_finddir to respect hidepid for direct PID access
Validation:
- make -j12: PASS
- make test-host: PASS (111/111)
- make test SMOKE_SMP=4: PASS (127/127)
- make test-battery: PASS (153/153)
- make analyzer: PASS
Addresses H2 from docs/URGENT_SECURITY_STATUS_2026-06-09.md
Tulio A M Mendes [Thu, 11 Jun 2026 00:48:33 +0000 (21:48 -0300)]
security: add tmpfs quotas and overflow hardening (H7)
- Add global tmpfs quota (512MB) to prevent DoS via memory exhaustion
- Track total memory usage across all tmpfs instances with g_tmpfs_total_used
- Validate quota before allocation in tmpfs_write_impl, tmpfs_add_file, and tmpfs_create_file
- Decrement quota when freeing memory in tmpfs_unlink_impl and tmpfs_kill_sb
- Add overflow protection in tmpfs_write_impl new_cap *= 2 loop:
- Cap per-file maximum at 256MB
- Check for overflow before doubling
- Return error if requested size exceeds max cap
- All quota operations protected by existing g_tmpfs_lock
Validation:
- make -j12: PASS
- make test-host: PASS (111/111)
- make test SMOKE_SMP=4: PASS (127/127)
- make test-battery: PASS (153/153)
- make analyzer: PASS
Addresses H7 from docs/URGENT_SECURITY_STATUS_2026-06-09.md
Tulio A M Mendes [Thu, 11 Jun 2026 00:46:26 +0000 (21:46 -0300)]
security: fix inverted user_range_ok checks in socket syscalls (M5)
- Fix SYSCALL_GETPEERNAME: user_range_ok == 0 means failure, not success
- Fix SYSCALL_GETSOCKNAME: user_range_ok == 0 means failure, not success
- Previously, the code incorrectly nested copy_to_user inside the
user_range_ok == 0 check, which would only execute copy_to_user
when the range check FAILED
- Now properly returns -EFAULT when user_range_ok fails, then
calls copy_to_user when the range check succeeds
Validation:
- make -j12: PASS
- make test-host: PASS (111/111)
- make test SMOKE_SMP=4: PASS (127/127)
- make test-battery: PASS (153/153)
- make analyzer: PASS
Addresses M5 from docs/URGENT_SECURITY_STATUS_2026-06-09.md
Tulio A M Mendes [Thu, 11 Jun 2026 00:44:47 +0000 (21:44 -0300)]
security: complete AIO validation (H1)
- Add fd mode validation in syscall_aio_rw_impl:
- Reject aio_read on O_WRONLY fd (except char devices)
- Reject aio_write on O_RDONLY fd (except char devices)
- Reject aio_write on MS_RDONLY mount
- Reuse same policy as read/write/pread/pwrite syscalls
- Fix fulltest AIO test to use O_RDWR instead of O_WRONLY
(test was incorrectly trying to read from write-only fd)
Validation:
- make -j12: PASS
- make test-host: PASS (111/111)
- make test SMOKE_SMP=4: PASS (127/127)
- make test-battery: PASS (153/153)
- make analyzer: PASS
Addresses H1 from docs/URGENT_SECURITY_STATUS_2026-06-09.md
Add an updated security status document replacing the outdated phase 2-5 checklist as the immediate planning reference.
The document classifies each previously listed item as implemented, partial, or still pending based on direct source inspection, and highlights the urgent remaining work around AIO validation, /proc hardening, futex robustness, fixed virtual addresses, tmpfs hardening, socket copyout review, and kernel RNG quality.
Fix the remote SIGKILL path so it no longer tears down a task on behalf of another CPU before that task reaches a safe exit point. SIGKILL is now queued as a fatal pending signal, forcibly unblocked, blocked/sleeping tasks are woken, and reschedule IPIs are sent so the target exits on its own CPU.
Fix CLONE_VM address-space accounting by using the global address-space refcount table to detect the first shared-mm clone and to decide teardown during reap. This closes the leak/counter skew when a non-leader thread creates another CLONE_VM thread.
Align clone semantics with the implementation by rejecting unsupported shared-state flags from the syscall interface, while still inheriting cwd, file references, descriptor flags, signal handlers, and signal mask by copy. Update pthread/fulltest users accordingly.
Also prevent userspace from changing SIGKILL/SIGSTOP disposition or blocking them, make default SIGKILL delivery fatal in usermode signal dispatch, and extend the kill regression test so the child tries to block SIGKILL before spinning.
Prevent process_waitpid() from reaping a zombie child while it is still the current process on another CPU. This avoids freeing a live process struct and kernel stack during the exit-to-schedule handoff under SMP, which matched the intermittent invalid-opcode panic seen after the job-control path in fulltest.
Also zero-initialize struct file allocations in the socket() and accept() paths so mount_root and any future fields cannot inherit heap garbage.
Validation: make -j$(nproc), make test-host, make test SMOKE_SMP=4, make test-battery (153/153 PASS).
fs: fix ext2 readdir visibility and ls single-file output
Corrected:
- fix ext2 directory iteration so readdir accepts valid exact-fit ext2 dirents instead of rejecting entries where name_len == rec_len - 8
- restore visibility of pre-existing files on ext2 mounts when listing directories with ls/getdents
- fix the userland ls command so path arguments are stat'ed first and handled as either directories or standalone files
- make 'ls -l /disk/outro.txt' and 'ls -l /disk2/outro.txt' print the expected single-file output instead of treating regular files like directories and producing no output
Changed:
- refactor ls formatting through a shared print_entry() helper reused by both directory listings and direct file-path listings
Validation:
- ext2 directory listing regression reproduced from mounted host image and corrected in src/kernel/ext2.c
- make test passes after the changes
fs: fix mounted ext2/fat stat and dirent regressions
Corrected:
- align kernel struct stat with the userspace ABI and update fulltest's local struct stat so stat/fstat copy the expected fields on mounted filesystems
- make getdents emit variable-length userspace dirent records with d_reclen and correct d_type values, and update ulibc readdir to consume them
- fix ext2 directory entry validation for exact-fit names and replace several 4KB on-stack buffers in ext2 inode/block/bitmap/write paths with heap-backed buffers to avoid stack-related crashes while listing and writing /disk
- populate sensible FAT mode/uid/gid defaults so ls -l reports sane metadata on FAT mounts
- preserve overlay root/initrd lifetime by keeping the initrd root registered separately and using a no-op close handler for the overlay root wrapper
Changed:
- add fulltest coverage for open('/disk') + getdents + close and for overlay root lifecycle after getdents/close/exec
- extend smoke_test.exp and test_battery.exp with the new /disk readdir and overlay root lifecycle expectations
- teach the minimal ulibc stdio formatter to accept a single 'l' length modifier so formats such as %lu work in ls -l output
TODO:
- add more targeted ext2 create/write regression coverage beyond the current fulltest/smoke battery if a smaller dedicated harness is introduced
- keep an eye on remaining direct getdents consumers, but the current tree passes make test and make test-battery in this state
Tulio A M Mendes [Tue, 26 May 2026 04:08:52 +0000 (01:08 -0300)]
init: integrate partition scanning after blockdev registration (Etapa 6)
- Added partition_scan_mbr() call for each ATA drive present
- Scans hda, hdb, hdc, hdd for MBR partitions
- Partitions are automatically registered during scan
- Tests: 124/124 PASS
Tulio A M Mendes [Tue, 26 May 2026 03:08:21 +0000 (00:08 -0300)]
vfs: implement cwd busy check via mount refcount
Implemented mount refcount-based busy check for umount to prevent
filesystem unmount when processes have cwd inside the mount.
Implementation uses mount refcount approach (same as Linux/BSD):
- Added vfs_mount_ref_by_path() and vfs_mount_unref_by_path()
- Functions use longest prefix match to find most specific mount
- chdir() updates refcounts (unref old cwd, ref new cwd)
- Process creation increments refcount for initial cwd
- Process exit decrements refcount for cwd
- vfs_umount_nolock() rejects if refcount > 0
This approach is O(1) for the check, avoids storing root as path
string (which caused struct alignment issues), and matches how
commercial OSes handle mount busy detection.
Test I20 (umount cwd) validates the implementation:
- Mounts tmpfs to /tmp/mnt_cwd
- Changes cwd into mount
- Verifies umount fails with -EBUSY
- Changes cwd back to /
- Verifies umount succeeds
Tulio A M Mendes [Tue, 26 May 2026 02:42:57 +0000 (23:42 -0300)]
vfs: add cwd check to umount to prevent filesystem in use
Added busy check in vfs_umount_nolock to reject unmount if any process
has its current working directory (cwd) within the mount being unmounted.
Implementation details:
- Checks current_process->cwd first (process executing umount)
- Iterates over ready_queue_head to check all other processes
- Uses path_is_mountpoint_prefix to determine if cwd is within mount
- Returns -EBUSY if any process has cwd in the mount
- Note: root directory check not implemented (would require vfs_getpath
or storing root as path in process struct)
Also added test I20 (umount cwd) to fulltest.c:
- Mounts tmpfs to /tmp/mnt_cwd
- Changes cwd into the mount
- Verifies umount fails with -EBUSY
- Changes cwd back to /
- Verifies umount succeeds
Updated test harnesses (smoke_test.exp, test_battery.exp) to include
the new test pattern.
Test results:
- Smoke test: 124/124 PASS
- Zero regressions
Tulio A M Mendes [Tue, 26 May 2026 02:20:43 +0000 (23:20 -0300)]
vfs: add cwd check to umount to prevent filesystem in use
Added busy check in vfs_umount_nolock to reject unmount if any process
has its current working directory (cwd) within the mount being unmounted.
Implementation details:
- Iterates over ready_queue_head under sched_lock
- Uses path_is_mountpoint_prefix to check if cwd is within mount
- Returns -EBUSY if any process has cwd in the mount
- Note: root directory check not implemented (root not stored as path in process struct)
This prevents crashes when processes attempt to access files after
their cwd filesystem has been unmounted.
Test results:
- Smoke test: 123/123 PASS
- Zero regressions
Tulio A M Mendes [Tue, 26 May 2026 01:54:53 +0000 (22:54 -0300)]
vfs: unify virtual filesystems in registry and add /dev/vda to devfs
- Added tmpfs_mount/tmpfs_kill_sb to tmpfs.c with proper cleanup
- Added devfs_mount/devfs_kill_sb to devfs.c (static globals, no cleanup needed)
- Added procfs_mount/procfs_kill_sb to procfs.c (static globals, no cleanup needed)
- Registered tmpfs, devfs, and procfs in filesystem type registry in init.c
- Added /dev/vda block device node to devfs for consistency with virtio-blk
- Updated headers (tmpfs.h, devfs.h, procfs.h) with VFS mount interface declarations
- Added necessary includes (fs.h, blockdev.h) to virtual filesystem implementations
Test results:
- Smoke test: 119/119 PASS
- Zero regressions
Tulio A M Mendes [Tue, 26 May 2026 01:51:06 +0000 (22:51 -0300)]
vfs: fix MS_REMOUNT, mount validation, blockdev locking, and resource leaks
High-severity fixes:
- MS_REMOUNT: pass full flags to VFS, store flags & ~MS_REMOUNT in remount branch
- Reject mount replacement with -EBUSY (except with MS_REMOUNT)
- Fix init_mount_fs leak: call kill_sb on failure after fst->mount
- Add MS_RDONLY check in SYSCALL_FTRUNCATE via f->mount_root
Medium-severity fixes:
- Centralize read-only check in vfs_link with vfs_require_writable_path
- Validate mountpoint in init_mount_fs and kconsole mount (must exist and be directory)
- Add spinlock to g_blockdevs with irqsave/irqrestore protection
- Remove const from blockdev_claim/release API (block_device_t* instead of const*)
Files modified:
- src/kernel/syscall.c: MS_REMOUNT fix, ftruncate readonly check
- src/kernel/fs.c: mount replacement rejection, vfs_link readonly check, bdev const removal
- src/kernel/init.c: mountpoint validation, init_mount_fs leak fix, blockdev_init_lock call
- src/kernel/kconsole.c: mountpoint validation
- include/blockdev.h: spinlock include, const removal from API
- src/kernel/blockdev.c: spinlock implementation, const removal from functions
- include/fs.h: const removal from bdev, mount function signatures
- include/ext2.h: const removal from bdev, mount signature
- include/fat.h: const removal from bdev, mount signature
- src/kernel/ext2.c: const removal from mount signature
- src/kernel/fat.c: const removal from mount signature
- src/drivers/virtio_blk.c: const removal from ops functions
- include/kernel/init.h: const removal from init_mount_fs signature
Test results:
- Smoke test: 119/119 PASS
- cppcheck: style-level warnings only (no errors)
- Revert proc_find_pid_safe to simple version (remove disabled UID check)
- Revert shm.c comment to original NX flag message
- Remove commented-out SOCK_RAW privilege check in socket.c
These checks were temporarily disabled in commit 63566ad to investigate
test failures but were never re-enabled. With NX support properly
implemented in commit 5d72805, all checks can now be active.
Tulio A M Mendes [Mon, 25 May 2026 21:12:34 +0000 (18:12 -0300)]
vfs: add vfs_require_writable_path checks to VFS mutator functions (P4.2)
- Add MS_RDONLY check to vfs_create, vfs_mkdir, vfs_unlink, vfs_rmdir
- Add MS_RDONLY check to vfs_rename for both old_path and new_path
- vfs_truncate already has the check from previous session
Tulio A M Mendes [Mon, 25 May 2026 21:05:48 +0000 (18:05 -0300)]
vfs: remove g_fat_root global, allocate root per mount (P3.1, P3.2, P3.3)
- Remove g_fat_root global from fat.c
- Allocate root node dynamically in fat_mount() and ext2_mount()
- Add root field to vfs_superblock_t for cleanup on umount
- Update fat_kill_sb and ext2_kill_sb to free root node
- Remove g_fat_root check from fat_close_impl
Tulio A M Mendes [Mon, 25 May 2026 21:01:29 +0000 (18:01 -0300)]
vfs: add kill_sb callback to vfs_fs_type_t for filesystem cleanup (P2.3)
- Add kill_sb function pointer to vfs_fs_type_t
- Update vfs_umount_nolock to call fstype->kill_sb instead of direct fat_umount/ext2_umount
- Implement fat_kill_sb and ext2_kill_sb callbacks in init.c
- Callbacks call filesystem-specific umount and free superblock
Tulio A M Mendes [Mon, 25 May 2026 20:58:12 +0000 (17:58 -0300)]
vfs: change mount API to return vfs_mount_result_t {root, sb} (P2.1)
- Add vfs_mount_result_t structure with root and superblock
- Update vfs_fs_type_t.mount() to return vfs_mount_result_t
- Update fat_mount() and ext2_mount() to build and return superblock
- Update init_mount_fs() to handle vfs_mount_result_t and set fstype in sb
- Update vfs_mount_full() to accept sb parameter
- Update all vfs_mount_full() callers to pass sb (NULL for virtual FS)
Tulio A M Mendes [Mon, 25 May 2026 20:22:46 +0000 (17:22 -0300)]
security: audit completion and TODO documentation
- Verified 23/25 items from SECURITY_FIX_PLAN_2026-05-25.md are implemented
- Documented K12/K13/K23 (/proc UID check) as TODO - requires UID infrastructure
- Documented K15 (raw socket privilege) as TODO - requires UID infrastructure
- Documented K24 (NX in SHM) as TODO - needs additional testing
- K24 NX flag temporarily disabled in shm_at for safety
- Analysis shows 92% completion of security fix plan
- Remaining items depend on multi-user authentication infrastructure
Tulio A M Mendes [Mon, 25 May 2026 19:57:13 +0000 (16:57 -0300)]
kernel: implement NX (No-Execute) support via IA32_EFER.NXE
Fix A01 (W^X/NX) which was deferred due to IA32_EFER.NXE MSR instability.
Root cause: NX bit was being set in PTEs without NXE enabled, causing
undefined behavior and kernel panic.
Changes:
- boot.S: Check CPUID.0x80000001:EDX bit 20 for NX support before enabling
- boot.S: Enable IA32_EFER.NXE (MSR 0xC0000080, bit 11) if NX supported
- vmm.c: Add g_nxe_enabled flag and check_nxe_enabled() function
- vmm.c: Conditionalize X86_PTE_NX usage based on g_nxe_enabled
- vmm.c: Print NX status in vmm_init()
- Makefile: Add -cpu qemu32,+nx to expose NX support in QEMU
- smoke_test.exp: Add -cpu qemu32,+nx for testing
Behavior:
- With NX support: NXE enabled, VMM uses NX bit for non-executable pages
- Without NX support: NXE not enabled, VMM ignores VMM_FLAG_NX
- W^X now works correctly for ELF loading, mmap/mprotect, etc.
K35: Add bounce buffers to sendmsg/recvmsg for SMAP compliance
- sendmsg: copy_from_user to kernel buffer before ksocket_send/ksocket_sendto
- recvmsg: ksocket_recvfrom to kernel buffer, then copy_to_user to user buffer
- Bounce buffer size limited to 4096 bytes per iov entry
- Ensures SMAP compliance by not passing user buffers directly to lwIP
A18: Fix shell command substitution syntax
- expand_vars was adding '(' at the start but missing ')' at the end
- Added closing parenthesis to properly wrap subshell command
- Changed cmd[1 + cmdlen] = '\0' to cmd[1 + cmdlen] = ')' and cmd[2 + cmdlen] = '\0'
K21: Implement O_NOFOLLOW flag in open/openat
- Added LOOKUP_FOLLOW and LOOKUP_NOFOLLOW flags to vfs_lookup_depth
- Added vfs_lookup_nofollow() function for O_NOFOLLOW path
- Modified syscall_open_impl to use vfs_lookup_nofollow when O_NOFOLLOW is set
- When O_NOFOLLOW is set, symlinks are not followed - returned as-is
A19: Implement mode checking in access() syscall
- F_OK: check file existence (already done)
- R_OK: assume readable if exists (simplified, no granular perms yet)
- W_OK: check mount read-only flag, return EROFS if read-only
- X_OK: check if file is regular file (FS_FILE), return EACCES if not
Tulio A M Mendes [Mon, 25 May 2026 19:16:48 +0000 (16:16 -0300)]
security: Round 5.5 posix_spawn PID fix (A13)
A13: Fix posix_spawn wrapper to preserve child PID
- Kernel copies child PID to *pid via copy_to_user
- Wrapper was overwriting *pid with return value (0 on success)
- Removed the line that overwrote *pid, kernel already filled it in
A17: Fix varargs handling in open/openat/fcntl
- open: Only read mode from varargs when O_CREAT is set
- openat: Only read mode from varargs when O_CREAT is set
- fcntl: Only read arg from varargs for commands that need it (F_DUPFD, F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_DUPFD_CLOEXEC)
- Prevents undefined behavior from reading varargs when not needed
Tulio A M Mendes [Mon, 25 May 2026 19:14:03 +0000 (16:14 -0300)]
security: Round 5.3 execl/execlp varargs (U04)
U04: Fix varargs handling in execl/execlp
- execl: Use va_list instead of pointer arithmetic for portability
- execlp: Use va_list instead of pointer arithmetic for portability
- Both functions now use __builtin_va_start/va_arg/va_end properly
U01: Secure temporary file creation
- mkstemp: Use /dev/urandom for randomness, fallback to pid+counter
- mkstemp: Use alphanumeric charset (62 chars) instead of only digits
- mkstemp: Always use O_CREAT|O_EXCL with mode 0600
- tmpfile: Use mkstemp for secure creation, unlink immediately for anonymity
- tmpnam: Use mkstemp for secure name generation, don't leave file around