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" |
| 37 | |
Emmanuel Gil Peyrot | deae98e | 2019-07-22 15:06:20 +0200 | [diff] [blame^] | 38 | /* These three functions are copied from shared/os-compatibility.c in order to |
| 39 | * behave like older clients, and allow ftruncate() to shrink the file’s size, |
| 40 | * so SIGBUS can still happen. |
| 41 | * |
| 42 | * There is no reason not to use os_create_anonymous_file() otherwise. */ |
| 43 | |
| 44 | #ifndef HAVE_MKOSTEMP |
| 45 | static int |
| 46 | set_cloexec_or_close(int fd) |
| 47 | { |
| 48 | if (os_fd_set_cloexec(fd) != 0) { |
| 49 | close(fd); |
| 50 | return -1; |
| 51 | } |
| 52 | return fd; |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | static int |
| 57 | create_tmpfile_cloexec(char *tmpname) |
| 58 | { |
| 59 | int fd; |
| 60 | |
| 61 | #ifdef HAVE_MKOSTEMP |
| 62 | fd = mkostemp(tmpname, O_CLOEXEC); |
| 63 | if (fd >= 0) |
| 64 | unlink(tmpname); |
| 65 | #else |
| 66 | fd = mkstemp(tmpname); |
| 67 | if (fd >= 0) { |
| 68 | fd = set_cloexec_or_close(fd); |
| 69 | unlink(tmpname); |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | return fd; |
| 74 | } |
| 75 | |
| 76 | static int |
| 77 | create_anonymous_file_without_seals(off_t size) |
| 78 | { |
| 79 | static const char template[] = "/weston-test-XXXXXX"; |
| 80 | const char *path; |
| 81 | char *name; |
| 82 | int fd; |
| 83 | int ret; |
| 84 | |
| 85 | path = getenv("XDG_RUNTIME_DIR"); |
| 86 | if (!path) { |
| 87 | errno = ENOENT; |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | name = malloc(strlen(path) + sizeof(template)); |
| 92 | if (!name) |
| 93 | return -1; |
| 94 | |
| 95 | strcpy(name, path); |
| 96 | strcat(name, template); |
| 97 | |
| 98 | fd = create_tmpfile_cloexec(name); |
| 99 | |
| 100 | free(name); |
| 101 | |
| 102 | if (fd < 0) |
| 103 | return -1; |
| 104 | |
| 105 | #ifdef HAVE_POSIX_FALLOCATE |
| 106 | do { |
| 107 | ret = posix_fallocate(fd, 0, size); |
| 108 | } while (ret == EINTR); |
| 109 | if (ret != 0) { |
| 110 | close(fd); |
| 111 | errno = ret; |
| 112 | return -1; |
| 113 | } |
| 114 | #else |
| 115 | do { |
| 116 | ret = ftruncate(fd, size); |
| 117 | } while (ret < 0 && errno == EINTR); |
| 118 | if (ret < 0) { |
| 119 | close(fd); |
| 120 | return -1; |
| 121 | } |
| 122 | #endif |
| 123 | |
| 124 | return fd; |
| 125 | } |
| 126 | |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 127 | /* tests, that attempt to crash the compositor on purpose */ |
| 128 | |
| 129 | static struct wl_buffer * |
| 130 | create_bad_shm_buffer(struct client *client, int width, int height) |
| 131 | { |
| 132 | struct wl_shm *shm = client->wl_shm; |
| 133 | int stride = width * 4; |
| 134 | int size = stride * height; |
| 135 | struct wl_shm_pool *pool; |
| 136 | struct wl_buffer *buffer; |
| 137 | int fd; |
| 138 | |
Emmanuel Gil Peyrot | deae98e | 2019-07-22 15:06:20 +0200 | [diff] [blame^] | 139 | fd = create_anonymous_file_without_seals(size); |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 140 | assert(fd >= 0); |
| 141 | |
| 142 | pool = wl_shm_create_pool(shm, fd, size); |
| 143 | buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, |
| 144 | WL_SHM_FORMAT_ARGB8888); |
| 145 | wl_shm_pool_destroy(pool); |
| 146 | |
| 147 | /* Truncate the file to a small size, so that the compositor |
| 148 | * will access it out-of-bounds, and hit SIGBUS. |
| 149 | */ |
| 150 | assert(ftruncate(fd, 12) == 0); |
| 151 | close(fd); |
| 152 | |
| 153 | return buffer; |
| 154 | } |
| 155 | |
Marek Chalupa | cfff312 | 2014-07-16 11:40:25 +0200 | [diff] [blame] | 156 | TEST(test_truncated_shm_file) |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 157 | { |
| 158 | struct client *client; |
| 159 | struct wl_buffer *bad_buffer; |
| 160 | struct wl_surface *surface; |
| 161 | int frame; |
| 162 | |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 163 | client = create_client_and_test_surface(46, 76, 111, 134); |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 164 | assert(client); |
| 165 | surface = client->surface->wl_surface; |
| 166 | |
| 167 | bad_buffer = create_bad_shm_buffer(client, 200, 200); |
| 168 | |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 169 | wl_surface_attach(surface, bad_buffer, 0, 0); |
| 170 | wl_surface_damage(surface, 0, 0, 200, 200); |
| 171 | frame_callback_set(surface, &frame); |
| 172 | wl_surface_commit(surface); |
Marek Chalupa | cfff312 | 2014-07-16 11:40:25 +0200 | [diff] [blame] | 173 | frame_callback_wait_nofail(client, &frame); |
| 174 | |
| 175 | expect_protocol_error(client, &wl_buffer_interface, |
| 176 | WL_SHM_ERROR_INVALID_FD); |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 177 | } |