Handle a failed read of the inotify event before comparing its signed ssize_t 
result with sizeof(struct inotify_event). This avoids treating -1 as a large 
unsigned length and inspecting the uninitialized event buffer.

Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Sahaj Chaudhari <[email protected]>
---
 tools/testing/selftests/cgroup/test_pids_local_events.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/cgroup/test_pids_local_events.c 
b/tools/testing/selftests/cgroup/test_pids_local_events.c
index fa501e209187..59885901fbf5 100644
--- a/tools/testing/selftests/cgroup/test_pids_local_events.c
+++ b/tools/testing/selftests/cgroup/test_pids_local_events.c
@@ -90,7 +90,11 @@ static int test_pids_events_notify(const char *root)
                goto cleanup;
        }
        n = read(inotify_fd, event_buf.bytes, sizeof(event_buf.bytes));
-       if (n < sizeof(struct inotify_event) ||
+       if (n < 0) {
+               reason = "failed to read pids.events notification";
+               goto cleanup;
+       }
+       if ((size_t)n < sizeof(struct inotify_event) ||
            !(event_buf.align.mask & IN_MODIFY)) {
                reason = "received no pids.events modify notification";
                goto cleanup;
-- 
2.43.0


Reply via email to