alpine 3.6
tmpfile weakness #5

4

Weakness Breakdown


Definition:

A temporary file weakness occurs when a temporary file that is created and used by a high-privilege process is accidentally shared with a low-privilege process, on account of it being temporary and generated after all security controls have been applied. This allows the low-privilege process to read data from the high-privilege process (information leakage), or worse, influence the high-privilege process by modifying the shared temporary file.

Warning code(s):

Temporary file race condition.

File Name:

gawk/src/gawk-4.1.4/vms/vms_popen.c

Context:

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

 #define expand_pipes(k) do {  PIPE *new_p; \
	int new_p_lim = ((k) / _NFILE + 1) * _NFILE; \
	emalloc(new_p, PIPE *, psize(new_p_lim), "expand_pipes"); \
	if (pipes_lim > 0) \
		memcpy(new_p, pipes, psize(pipes_lim)),  free(pipes); \
	memset(new_p + psize(pipes_lim), 0, psize(new_p_lim - pipes_lim)); \
	pipes = new_p,  pipes_lim = new_p_lim;  } while(0)

FILE *
popen( const char *command, const char *mode )
{
    FILE *current;
    char *name;
    int   cur;
    pipemode curmode;

    if (strcmp(mode, "r") == 0)
	curmode = reading;
    else if (strcmp(mode, "w") == 0)
	curmode = writing;
    else
	return NULL;

    /* make a name for the temporary file */
    if ((name = mktemp(strdup("sys$ scratch:gawk-pipe_XXXXXX.tmp"))) == 0)
	return NULL;

    if (curmode == reading) {
	/* an input pipe reads a temporary file created by the command */
	vms_execute(command, (char *)0, name);	/* 'command >tempfile' */
    }
    if ((current = fopen(name, mode, "mbc=24", "mbf=2")) == NULL) {
	free(name);
	return NULL;
    }
    cur = fileno(current);
    if (cur >= pipes_lim)  expand_pipes(cur);
 /* assert( cur >= 0 && cur < pipes_lim ); */
    pipes[cur].name = name;
    pipes[cur].pmode = curmode;
    pipes[cur].command = strdup(command);
    return current;
}

int
pclose( FILE *current )
{
    int rval, cur = fileno(current);

 /* assert( cur >= 0 && cur < pipes_lim ); */ 

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.