Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * Copyright © 2013 Collabora, Ltd. |
| 4 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 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: |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 12 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 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. |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 25 | */ |
| 26 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 27 | #include "config.h" |
| 28 | |
Emmanuel Gil Peyrot | deae98e | 2019-07-22 15:06:20 +0200 | [diff] [blame] | 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 33 | #include <unistd.h> |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 34 | |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 35 | #include "shared/os-compatibility.h" |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 36 | #include "weston-test-client-helper.h" |
Pekka Paalanen | 701676d | 2019-11-13 15:45:10 +0200 | [diff] [blame] | 37 | #include "weston-test-fixture-compositor.h" |
| 38 | |
| 39 | static enum test_result_code |
| 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); |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 49 | |
Emmanuel Gil Peyrot | deae98e | 2019-07-22 15:06:20 +0200 | [diff] [blame] | 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 |
| 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 |
| 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 |
| 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 | |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 139 | /* tests, that attempt to crash the compositor on purpose */ |
| 140 | |
| 141 | static struct wl_buffer * |
| 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 | |
Emmanuel Gil Peyrot | deae98e | 2019-07-22 15:06:20 +0200 | [diff] [blame] | 151 | fd = create_anonymous_file_without_seals(size); |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 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 | |
Marek Chalupa | cfff312 | 2014-07-16 11:40:25 +0200 | [diff] [blame] | 168 | TEST(test_truncated_shm_file) |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 169 | { |
| 170 | struct client *client; |
| 171 | struct wl_buffer *bad_buffer; |
| 172 | struct wl_surface *surface; |
Pekka Paalanen | 819054c | 2021-06-11 16:54:12 +0300 | [diff] [blame^] | 173 | struct wl_callback *frame_cb; |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 174 | int frame; |
| 175 | |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 176 | client = create_client_and_test_surface(46, 76, 111, 134); |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 177 | assert(client); |
| 178 | surface = client->surface->wl_surface; |
| 179 | |
| 180 | bad_buffer = create_bad_shm_buffer(client, 200, 200); |
| 181 | |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 182 | wl_surface_attach(surface, bad_buffer, 0, 0); |
| 183 | wl_surface_damage(surface, 0, 0, 200, 200); |
Pekka Paalanen | 819054c | 2021-06-11 16:54:12 +0300 | [diff] [blame^] | 184 | frame_cb = frame_callback_set(surface, &frame); |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 185 | wl_surface_commit(surface); |
Pekka Paalanen | 819054c | 2021-06-11 16:54:12 +0300 | [diff] [blame^] | 186 | if (!frame_callback_wait_nofail(client, &frame)) |
| 187 | wl_callback_destroy(frame_cb); |
Marek Chalupa | cfff312 | 2014-07-16 11:40:25 +0200 | [diff] [blame] | 188 | |
| 189 | expect_protocol_error(client, &wl_buffer_interface, |
| 190 | WL_SHM_ERROR_INVALID_FD); |
Pekka Paalanen | 819054c | 2021-06-11 16:54:12 +0300 | [diff] [blame^] | 191 | |
| 192 | wl_buffer_destroy(bad_buffer); |
| 193 | client_destroy(client); |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 194 | } |