Project

General

Profile

Bug #623 ยป fix-sigbus-race.patch

Mathieu Desnoyers, 11/06/2013 07:16 AM

View differences:

src/bin/lttng-sessiond/shm.c
48 48
	/* Default permissions */
49 49
	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
50 50

  
51
	/* Change owner of the shm path */
51
	/*
52
	 * Change owner of the shm path.
53
	 */
52 54
	if (global) {
53
		ret = chown(shm_path, 0, 0);
54
		if (ret < 0) {
55
			if (errno != ENOENT) {
56
				PERROR("chown wait shm");
57
				goto error;
58
			}
59
		}
60

  
61 55
		/*
62
		 * If global session daemon, any application can register so the shm
63
		 * needs to be set in read-only mode for others.
56
		 * If global session daemon, any application can
57
		 * register. Make it initially writeable so applications
58
		 * registering concurrently can do ftruncate() by
59
		 * themselves.
64 60
		 */
65
		mode |= S_IROTH;
66
	} else {
67
		ret = chown(shm_path, getuid(), getgid());
68
		if (ret < 0) {
69
			if (errno != ENOENT) {
70
				PERROR("chown wait shm");
71
				goto error;
72
			}
73
		}
74
	}
75

  
76
	/*
77
	 * Set permissions to the shm even if we did not create the shm.
78
	 */
79
	ret = chmod(shm_path, mode);
80
	if (ret < 0) {
81
		if (errno != ENOENT) {
82
			PERROR("chmod wait shm");
83
			goto error;
84
		}
61
		mode |= S_IROTH | S_IWOTH;
85 62
	}
86 63

  
87 64
	/*
......
107 84
	}
108 85

  
109 86
#ifndef __FreeBSD__
110
	ret = fchmod(wait_shm_fd, mode);
111
	if (ret < 0) {
112
		PERROR("fchmod");
113
		exit(EXIT_FAILURE);
87
	if (global) {
88
		ret = fchown(wait_shm_fd, 0, 0);
89
		if (ret < 0) {
90
			PERROR("fchown");
91
			exit(EXIT_FAILURE);
92
		}
93
		/*
94
		 * If global session daemon, any application can
95
		 * register so the shm needs to be set in read-only mode
96
		 * for others.
97
		 */
98
		mode &= ~S_IWOTH;
99
		ret = fchmod(wait_shm_fd, mode);
100
		if (ret < 0) {
101
			PERROR("fchmod");
102
			exit(EXIT_FAILURE);
103
		}
104
	} else {
105
		ret = fchown(wait_shm_fd, getuid(), getgid());
106
		if (ret < 0) {
107
			PERROR("fchown");
108
			exit(EXIT_FAILURE);
109
		}
114 110
	}
115 111
#else
116
#warning "FreeBSD does not support setting file mode on shm FD. Remember that for secure use, lttng-sessiond should be started before applications linked on lttng-ust."
112
#warning "FreeBSD does not support setting file mode on shm FD."
117 113
#endif
118 114

  
119 115
	DBG("Got the wait shm fd %d", wait_shm_fd);
    (1-1/1)