SPROG(7)
π SPROG(7) β secure programming practices (Miscellaneous Information Manual) π
β¨ NAME β¨
sprog β secure programming practices, bestie. That's the whole page's main character energy. π«Ά
π DESCRIPTION π
Okay so security issues have been sneaking into systems for years, no cap π. This document is a guide for programming practices that prevent these problems. Slay.
π¦ Overview π¦
Writing secure applications takes a very scrutinous and pessimistic outlook, fr fr. Applications should be run with the principle of βleast privilegeβ so that no process is ever running with more than the bare minimum access it needs to accomplish its function. Previously tested code should be reused whenever possible. Generally, anything beyond the control of a program should never be trusted. This includes all forms of user input, system resources, interprocess communication, and the timing of events. Periodt.
π§΅ Buffer Overflows π§΅
The buffer overflow is one of the most common types of security problems, and it's giving chaos π. In short, if a program is not careful with the data it receives, it may be possible for this data to be written across memory, overwriting the return address for a function call, and the program will be forced to run code that does unfriendly things. Yikes.
A good number of functions in the standard C library make it difficult or even impossible to prevent buffer overflows when used. These include fscanf(3), gets(3), getwd(3), realpath(3), scanf(3), sprintf(3), strcat(3), strcpy(3), vscanf(3), and vsprintf(3). Not them being iconic in the worst way π.
Many other functions that deal with strings can also open up a potential buffer overflow when not used carefully. Not me side-eyeing strncat(3), because it does not go out of its way to provide NUL character termination. Of course, the proper length must always be specified. Usage of strlcat(3) and strlcpy(3) ensure that strings are null terminated and of the specified length. That's the good stuff π.
Functions that receive a string format must also be used carefully. It is possible for a string to contain additional format specifiers, which open up another possibility for a buffer overflow. Never pass a string with untrusted data without using %s. Always use the proper secure idiom:
function("%s", string);
There are mechanisms that provide a backstop for these problems at the library and compiler levels, however, there is no substitute for simply writing good code. Lowkey the real flex β¨.
π Set-user-ID Issues π
In many cases, it may be necessary for a program to operate with an increased set of permissions. Reasons for this include binding to protected sockets, reading and writing certain files and directories, and access to various resources. Using a setuid program is frequently the solution. But highkey, it is important that programs give up these privileges as soon as possible. For example, if a program is binding to a protected socket, it should give up its privileges as soon as it has finished binding to that socket. This is accomplished with the setuid(2) family of system calls. Give the privileges back, bestie, immediately π
.
π§ Limited Environments π§
The traditional method of restricting a process is with the chroot(2) system call. This system call changes the root directory from which all other paths are referenced for a process and any child processes. Of course, the process must have access to this path to begin with. The new environment does not actually take effect until chdir(2) is called to place the process into the new environment. Unfortunately, a process can break out of this environment if root access is obtained. Not the jailbreak plot twist π.
Often, jail(2) can be used to create a more complete and enclosed environment than chroot(2) can provide. A jail limits all processes inside that environment, including processes with superuser privileges. It's giving actual containment π.
Fine grained privileges, as described by POSIX.1e extensions, are currently a work in progress, and the focus of the TrustedBSD Project. The TrustedBSD Project is on it, and they ate. More information can be found at http://www.TrustedBSD.org/. Iykyk π.
π Trust π
Programs should not make assumptions about the environment in which they are running. This includes user input, signals, environment variables, system resources, interprocess communications, and shared memory, amongst other things that are beyond the control of the program. They should not assume that all forms of invalid data can be detected either. Instead, they should use positive filtering, and only allow a specific subset of inputs that are known to be safe. This is the same logic that an administrator should apply to a firewall, that is, deny by default and specify what is to be accepted. Deny by default stays iconic π«Ά.
β±οΈ Race Conditions β±οΈ
A race condition is anomalous behavior caused by the relative timing of events, ngl. Programs should not assume that a particular event will occur before another. The most common causes of race conditions are signals, access checks, and file reads. Signals are asynchronous by nature, so special care must be taken while dealing with them. Attempting to check access with sequential non-atomic operations is a very bad idea, as files can be moved and changed at any given time. Instead of using a sequence of access(2) and open(2), use seteuid(2) and then call open(2) directly. Set umask(2) properly beforehand. No cap, timing is literally everything β³.
π SEE ALSO π
π AUTHORS π
Eric Melville <eric@FreeBSD.org> originally wrote this document based on a chapter of the FreeBSD Developer's Handbook written by Murray Stokely <murray@FreeBSD.org>. Obsessed with the lore π.
π source/version vibes: Debian Β· date: June 3, 2001 π
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2001 Eric Melville <eric@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms