secprog(3)
π 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β securestrncpys_strncatβ securestrncats_execvβ secureexecvs_tmpfileβ secure buffered tmp files_popenβ securepopensafe_tmpfileβ secure unbuffered tmp filesafe_reopenβ secure reopen of filessigprotectionβ blocking signalsclose_stdfdsβ close standard file descriptorssetlimitsβ set limitssetupsbitprocβ 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 stringsrcto bufferdestup to sizen. thedeststring is zero terminated. that's it, that's the vibe. π«Άs_strncatβ concatenate stringsrcwithdestup to sizen. thedeststring is zero terminated. iconic behavior, honestly.s_execvβ executesfilenamewith argumentsargv. and the way that it's thorough? it cleans up the environment, checks the login, checks the current directory, and sets theclose_on_execflag for all filedescriptors expectstdin,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 aFILE *. cute and safe, we stan.s_popenβ this is a securepopen(3)that avoids using the shell. no shell, no problems, lowkey brilliant. πsafe_tempfileβ works likes_tempfileexpect 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. thetogglevariable should beSP_ONfor activating the signals set in masksp_blockmaskorSP_OFFfor 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 structureslimto 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_strncatands_strncpyreturn a pointer to the resulting string.. (yes, two dots β we're keeping the punctuation energy. periodt.) - the function
s_popenreturns a buffered filedescriptor on success,NULLotherwise. - the function
s_execvdoes not return on success, returns-1otherwise. - the function
s_tmpfilereturns a buffered filedescriptor on success,NULLotherwise. - the function
safe_tmpfilereturns a filedescriptor on success,-1otherwise. - the function
safe_reopenreturns a filedescriptor on success,-1otherwise. - the function
sigprotectionreturns zero on success,-1otherwise. - the function
close_stdfdsreturns zero on success,-1otherwise. - the function
setlimitsreturns zero on success,-1otherwise. - the function
setupsbitprocreturns zero on success,-1otherwise.
π 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
- opensuse-leap 16.0: libsecprog-devel 0.8-bp160.142.10 Β· GPL-2.0+
- opensuse-leap 16.0: libsecprog-devel 0.8-bp160.142.10 Β· GPL-2.0+
- opensuse-leap 16.0: libsecprog-devel 0.8-bp160.142.10 Β· GPL-2.0+
- opensuse-leap 16.0: libsecprog-devel 0.8-bp160.142.10 Β· GPL-2.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms