client: add checking fd is valid 85/252285/1
authorChangyeon Lee <cyeon.lee@samsung.com>
Wed, 30 Dec 2020 07:32:18 +0000 (16:32 +0900)
committerChangyeon Lee <cyeon.lee@samsung.com>
Tue, 26 Jan 2021 06:48:34 +0000 (15:48 +0900)
Change-Id: I0a0da706f709bc85daa8c707856695702ad7e773

src/wayland-tbm-client.c

index 0d82113..c13d7db 100644 (file)
@@ -458,10 +458,28 @@ _wayland_tbm_client_find_tbm_buffer_surface(struct wayland_tbm_client *tbm_clien
        return NULL;
 }
 
+static int
+_wayland_tbm_client_fd_is_valid(int fd)
+{
+       long flags;
+
+       if (fd < 0) return 0;
+
+       flags = fcntl(fd, F_GETFD);
+       if (flags < 0) {
+               WL_TBM_LOG_E("fd(%d) is invalid(%m)", fd);
+               close(fd);
+               return 0;
+       }
+
+       return 1;
+}
+
 static struct wl_buffer *
 _wayland_tbm_client_create_wl_buffer(struct wayland_tbm_client *tbm_client, tbm_surface_h surface, int with_fd)
 {
        int bufs[TBM_SURF_PLANE_MAX] = { -1, -1, -1, -1};
+       int buf = -1;
        struct wl_buffer *wl_buffer = NULL;
        int num_buf, i;
        tbm_surface_info_s info = {0, };
@@ -492,16 +510,23 @@ _wayland_tbm_client_create_wl_buffer(struct wayland_tbm_client *tbm_client, tbm_
                        goto err;
                }
 
-               if (with_fd)
-                       bufs[i] = tbm_bo_export_fd(bo);
-               else
-                       bufs[i] = tbm_bo_export(bo);
+               if (with_fd) {
+                       buf = tbm_bo_export_fd(bo);
+                       if (!_wayland_tbm_client_fd_is_valid(buf)) {
+                               goto err;
+                       }
+               }
+               else {
+                       buf = tbm_bo_export(bo);
+               }
 
-               if (((with_fd) && (bufs[i] < 0)) ||
-                   ((!with_fd) && (bufs[i] <= 0))) {
+               if (((with_fd) && (buf < 0)) ||
+                   ((!with_fd) && (buf <= 0))) {
                        WL_TBM_LOG_E("Failed to export(with_fd:%d, bufs:%d)", with_fd, bufs[i]);
                        goto err;
                }
+
+               bufs[i] = buf;
        }
 
        wl_array_init(&plane_buf_idx);