linux-sysprog · intermediate · ~15 min
Validate Unix-socket peer credentials before processing the protocol.
You've just accepted a Unix-socket connection. SO_PEERCRED gave you the
peer's (pid, uid, gid). Implement
int allow_peer(int peer_uid, const int *allow, int n_allow) returning 1 if
peer_uid is in the allow-list array, else 0. Allow uid 0 (root) ONLY when
0 is explicitly in the list.
Unix socket connections carry the peer's real uid via SO_PEERCRED. Refusing connections by uid is the cheapest in-protocol authentication.
Peer uid + allow-list.
0/1.
Allow only when uid is in list. No implicit root.
int allow_peer(int peer_uid, const int *allow, int n_allow) { /* TODO */ (void)peer_uid; (void)allow; (void)n_allow; return 0; }
Special-casing uid==0 as 'always allow' — root must be explicitly listed.
Empty allow-list (deny all). Negative uid (invalid; deny).
O(n_allow).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.