alpine 3.6
format weakness #58


Weakness Breakdown


A format string exploit occurs when the data of an input string is evaluated as a command by the program. This class of attacks is very similar to buffer overflows since an attacker could execute code, read the stack or cause new behaviors that compromise security. Learn more about format string attacks on OWASP attack index.

Warning code(s):

If format strings can be influenced by an attacker, they can be exploited.

File Name:



The highlighted line of code below is the trigger point of this particular Alpine 3.6 format weakness.

 	if (sigaction(signum, &nact, &oact) != 0)
		return SIG_ERR;
	return oact.sa_handler;

static int 
scan_mode(const char *mode, int *omode)

	if (!strcmp(mode, "r")) {
		*omode = O_RDONLY;
	} else if (!strcmp(mode, "w")) {
		*omode = O_WRONLY | O_CREAT | O_TRUNC;
	} else if (!strcmp(mode, "wx")) {
		*omode = O_WRONLY | O_CREAT | O_EXCL;
	} else if (!strcmp(mode, "a")) {
		*omode = O_WRONLY | O_APPEND | O_CREAT;
	} else if (!strcmp(mode, "a+")) {
		*omode = O_RDWR | O_APPEND;
	} else if (!strcmp(mode, "r+")) {
		*omode = O_RDWR;
	} else if (!strcmp(mode, "w+")) {
		*omode = O_RDWR   | O_CREAT | O_EXCL;
	} else {
		fprintf(stderr, catgets(catd, CATSET, 152,
			"Internal error: bad stdio open mode %s\n"), mode);
		errno = EINVAL;
		return -1;
	return 0;

safe_fopen(const char *file, const char *mode, int *omode)
	int  fd;

	if (scan_mode(mode, omode) < 0) 
		return NULL;
	if ((fd = open(file, *omode, 0666)) < 0)
		return NULL;
	return fdopen(fd, mode);

Fopen(const char *file, const char *mode)
	FILE *fp;
	int omode;

The registered trademark Linux® is used pursuant to a sublicense from the Linux Foundation, the exclusive licensee of Linus Torvalds, owner of the mark on a world­wide basis.