alpine 3.7
race weakness #4

5

Weakness Breakdown


Definition:

A race condition exists when parallel code accesses shared data without proper coordination. An attack that uses a race-condition weakness takes advantage of the unsafe data access to manipulate how one of the parallel sections of code reacts. Even though each process runs as intended, the outcome is unexpected. For example, consider a bank service that depends on an encryption key that it reads from a known location. An independent cryptography service is responsible for generating the key and placing it where the bank is expected to read it in a timely manner. If the bank and cryptography services do not coordinate with each other, then the bank may read a blank encryption key before cryptography writes the key to the location. This can effectively turn off all encryption for the bank without either service, or the administrator, knowing that something has gone wrong.

Warning code(s):

This accepts filename arguments; if an attacker can move those files, a race condition results..

File Name:

gphoto2/src/gphoto2-2.5.14/gphoto2/main.c

Context:

The highlighted line of code below is the trigger point of this particular Alpine 3.7 race weakness.

 				in_fd = open(curname, O_RDONLY);
				if (in_fd < 0)
					perror("Can't open file for reading");

				out_fd = open(s, O_CREAT | O_WRONLY, 0644);
				if (out_fd < 0)
					perror("Can't open file for writing");

				while (1) {
					ssize_t result = read(in_fd, buf, sizeof(buf));
					if (!result) break;
					if (-1 == write(out_fd, buf, result)) {
						perror("write");
						break;
					}
				}
				close(out_fd);
				close(in_fd);
				unlink(curname);
			} else
				perror("rename");
		}
		x = umask(0022); /* get umask */
		umask(x);/* set it back to the old value */
		chmod(s,0666 & ~x);
	}
	res = gp_file_get_mtime (file, &mtime);
        if ((res == GP_OK) && (mtime)) {
                u.actime = mtime;
                u.modtime = mtime;
                utime (s, &u);
        }
	gp_params_run_hook(&gp_params, "download", s);
	return (GP_OK);
}

int
camera_file_exists (Camera *camera, GPContext *context, const char *folder,
		    const char *filename, CameraFileType type)
{
	CameraFileInfo info;
	CR (gp_camera_file_get_info (camera, folder, filename, &info,
				     context));
	switch (type) {
	case GP_FILE_TYPE_METADATA:
		return TRUE;
	case GP_FILE_TYPE_AUDIO:
		return (info.audio.fields != 0);
	case GP_FILE_TYPE_PREVIEW:
		return (info.preview.fields != 0); 

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.