secprog(3)

πŸ“¦ version 0.8, in opensuse-leap 16.0

πŸ’… secprog(3) β€” Secure Programming Library, but make it understandable

okay bestie, listen up β€” this is the secprog(3) page from the Secure Programming Library, and it's lowkey a whole buffet of secure replacements for the sketchy libc functions you'd otherwise use. it's giving "responsible adult energy" and honestly? we love that for us. πŸ’–


✨ NAME ✨

so here's the cast of characters, all from secprog:

  • s_strncpy β€” secure strncpy
  • s_strncat β€” secure strncat
  • s_execv β€” secure execv
  • s_tmpfile β€” secure buffered tmp file
  • s_popen β€” secure popen
  • safe_tmpfile β€” secure unbuffered tmp file
  • safe_reopen β€” secure reopen of files
  • sigprotection β€” blocking signals
  • close_stdfds β€” close standard file descriptors
  • setlimits β€” set limits
  • setupsbitproc β€” setup at beginning of setuid code

πŸŽ€ SYNOPSIS πŸŽ€

like, grab the header first, obviously:

#include <secprog.h>

and then all the signatures, verbatim, because we do not play with prototypes:

char *s_strncpy(char *dest, char *src, size_t n);

char *s_strncat(char *dest, char *src, size_t n);

int s_execv(const char *filename, char *const argv[]);

FILE *s_tmpfile(void);

FILE *s_popen(char *cmd, const char *type);

int safe_tmpfile(char *filename);

int safe_reopen(char *file, int mode);

int sigprotection(u_int toggle, sigset_t *sp_blockmask);

int close_stdfds(void);

int setlimits(sl_limit slim);

int setupsbitproc(void);

🌸 DESCRIPTION 🌸

okay so let's go function by function, no skipping, periodt.

  • s_strncpy β€” copys string src to buffer dest up to size n. the dest string is zero terminated. that's it, that's the vibe. 🫢
  • s_strncat β€” concatenate string src with dest up to size n. the dest string is zero terminated. iconic behavior, honestly.
  • s_execv β€” executes filename with arguments argv. and the way that it's thorough? it cleans up the environment, checks the login, checks the current directory, and sets the close_on_exec flag for all filedescriptors expect stdin, stdout, stderr. (yes, "expect" β€” that's what the page says, don't come for me 😭). it's giving main character security energy. πŸ”₯
  • s_tmpfile β€” creates a temp file more securely and returns a FILE *. cute and safe, we stan.
  • s_popen β€” this is a secure popen(3) that avoids using the shell. no shell, no problems, lowkey brilliant. πŸ’…
  • safe_tempfile β€” works like s_tempfile expect that it returns an integer as filedescriptor. (again, "expect" β€” preserving the lore exactly as written, bestie. don't @ me.) πŸ™ˆ
  • safe_reopen β€” reopens a file securely even in world-writeable directories like /tmp. the way that it protects you in the wild west of /tmp? obsessed. πŸ¦‹
  • sigprotection β€” should be called before and after a critical code part. the toggle variable should be SP_ON for activating the signals set in mask sp_blockmask or SP_OFF for switching signal blocking off. so basically: wrap your critical section like a little security blanket. 🧚
  • close_stdfds β€” just closes all standard filedescriptors. (Note: Just needed on systems with old glibc, or old OpenBSD kernel.) so like, vintage systems only, iykyk. πŸ’€
  • setlimits β€” uses the structure slim to set process limites like: RLIMIT_MEMLOCK, RLIMIT_NPROC, RLIMIT_NOFILE, RLIMIT_RSS, RLIMIT_CORE, RLIMIT_STACK, RLIMIT_DATA, RLIMIT_FSIZE. and yes, "limites" β€” the page said it, I'm keeping it. here's the struct in all its glory:
typedef struct
{
  int fsize;          /* max file size */
  int data;           /* max data size */
  int stack;          /* max stack size */
  int core;           /* max core file size */
  int rss;            /* max resident set size */
  int nproc;          /* max number of processes */
  int nofile;         /* max number of open files */
  int memlock;        /* max locked-in-memory address space */
} sl_limit;
  • setupsbitproc β€” should be called at the beginning of every setuid application to close the standard filedescriptors and to set some process limits. (Note: It does not drop setuid privileges.) so important, bestie β€” it's a setup step, not a full privilege drop. don't get it twisted. ⚠️

πŸ¦‹ RETURN VALUE πŸ¦‹

okay so what do you get back? let's break it down:

  • the function s_strncat and s_strncpy return a pointer to the resulting string.. (yes, two dots β€” we're keeping the punctuation energy. periodt.)
  • the function s_popen returns a buffered filedescriptor on success, NULL otherwise.
  • the function s_execv does not return on success, returns -1 otherwise.
  • the function s_tmpfile returns a buffered filedescriptor on success, NULL otherwise.
  • the function safe_tmpfile returns a filedescriptor on success, -1 otherwise.
  • the function safe_reopen returns a filedescriptor on success, -1 otherwise.
  • the function sigprotection returns zero on success, -1 otherwise.
  • the function close_stdfds returns zero on success, -1 otherwise.
  • the function setlimits returns zero on success, -1 otherwise.
  • the function setupsbitproc returns zero on success, -1 otherwise.

🌈 SEE ALSO 🌈

go touch grass and read these too, bestie:

strncpy(3), strncat(3), popen(3), exec(3).


okay that's the whole page, and honestly? you ate that. πŸ’…βœ¨

this absolute icon of a manpage was brought to you by Thomas Biege <thomas@suse.de>, dated 22 Apr 2004 β€” a whole legend, no cap. πŸ«ΆπŸ‘‘

πŸ“¦ where this page comes from