• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2013 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 #include "config.h"
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include "shared/os-compatibility.h"
36 #include "weston-test-client-helper.h"
37 #include "weston-test-fixture-compositor.h"
38 
39 static enum test_result_code
fixture_setup(struct weston_test_harness * harness)40 fixture_setup(struct weston_test_harness *harness)
41 {
42 	struct compositor_setup setup;
43 
44 	compositor_setup_defaults(&setup);
45 
46 	return weston_test_harness_execute_as_client(harness, &setup);
47 }
48 DECLARE_FIXTURE_SETUP(fixture_setup);
49 
50 /* These three functions are copied from shared/os-compatibility.c in order to
51  * behave like older clients, and allow ftruncate() to shrink the file’s size,
52  * so SIGBUS can still happen.
53  *
54  * There is no reason not to use os_create_anonymous_file() otherwise. */
55 
56 #ifndef HAVE_MKOSTEMP
57 static int
set_cloexec_or_close(int fd)58 set_cloexec_or_close(int fd)
59 {
60 	if (os_fd_set_cloexec(fd) != 0) {
61 		close(fd);
62 		return -1;
63 	}
64 	return fd;
65 }
66 #endif
67 
68 static int
create_tmpfile_cloexec(char * tmpname)69 create_tmpfile_cloexec(char *tmpname)
70 {
71 	int fd;
72 
73 #ifdef HAVE_MKOSTEMP
74 	fd = mkostemp(tmpname, O_CLOEXEC);
75 	if (fd >= 0)
76 		unlink(tmpname);
77 #else
78 	fd = mkstemp(tmpname);
79 	if (fd >= 0) {
80 		fd = set_cloexec_or_close(fd);
81 		unlink(tmpname);
82 	}
83 #endif
84 
85 	return fd;
86 }
87 
88 static int
create_anonymous_file_without_seals(off_t size)89 create_anonymous_file_without_seals(off_t size)
90 {
91 	static const char template[] = "/weston-test-XXXXXX";
92 	const char *path;
93 	char *name;
94 	int fd;
95 	int ret;
96 
97 	path = getenv("XDG_RUNTIME_DIR");
98 	if (!path) {
99 		errno = ENOENT;
100 		return -1;
101 	}
102 
103 	name = malloc(strlen(path) + sizeof(template));
104 	if (!name)
105 		return -1;
106 
107 	strcpy(name, path);
108 	strcat(name, template);
109 
110 	fd = create_tmpfile_cloexec(name);
111 
112 	free(name);
113 
114 	if (fd < 0)
115 		return -1;
116 
117 #ifdef HAVE_POSIX_FALLOCATE
118 	do {
119 		ret = posix_fallocate(fd, 0, size);
120 	} while (ret == EINTR);
121 	if (ret != 0) {
122 		close(fd);
123 		errno = ret;
124 		return -1;
125 	}
126 #else
127 	do {
128 		ret = ftruncate(fd, size);
129 	} while (ret < 0 && errno == EINTR);
130 	if (ret < 0) {
131 		close(fd);
132 		return -1;
133 	}
134 #endif
135 
136 	return fd;
137 }
138 
139 /* tests, that attempt to crash the compositor on purpose */
140 
141 static struct wl_buffer *
create_bad_shm_buffer(struct client * client,int width,int height)142 create_bad_shm_buffer(struct client *client, int width, int height)
143 {
144 	struct wl_shm *shm = client->wl_shm;
145 	int stride = width * 4;
146 	int size = stride * height;
147 	struct wl_shm_pool *pool;
148 	struct wl_buffer *buffer;
149 	int fd;
150 
151 	fd = create_anonymous_file_without_seals(size);
152 	assert(fd >= 0);
153 
154 	pool = wl_shm_create_pool(shm, fd, size);
155 	buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
156 					   WL_SHM_FORMAT_ARGB8888);
157 	wl_shm_pool_destroy(pool);
158 
159 	/* Truncate the file to a small size, so that the compositor
160 	 * will access it out-of-bounds, and hit SIGBUS.
161 	 */
162 	assert(ftruncate(fd, 12) == 0);
163 	close(fd);
164 
165 	return buffer;
166 }
167 
TEST(test_truncated_shm_file)168 TEST(test_truncated_shm_file)
169 {
170 	struct client *client;
171 	struct wl_buffer *bad_buffer;
172 	struct wl_surface *surface;
173 	int frame;
174 
175 	client = create_client_and_test_surface(46, 76, 111, 134);
176 	assert(client);
177 	surface = client->surface->wl_surface;
178 
179 	bad_buffer = create_bad_shm_buffer(client, 200, 200);
180 
181 	wl_surface_attach(surface, bad_buffer, 0, 0);
182 	wl_surface_damage(surface, 0, 0, 200, 200);
183 	frame_callback_set(surface, &frame);
184 	wl_surface_commit(surface);
185 	frame_callback_wait_nofail(client, &frame);
186 
187 	expect_protocol_error(client, &wl_buffer_interface,
188 			      WL_SHM_ERROR_INVALID_FD);
189 }
190