summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2022-03-24 17:58:07 +0900
committerLuca Boccassi <luca.boccassi@gmail.com>2022-03-24 23:12:34 +0000
commit00adc340bb15bc9d634db6caa48f1c964b99f79a (patch)
tree26c4b95de937589754f9db91cdba6283b16db150
parentf72f8021182f930bb86ff4e3a05b8f09fa7d3179 (diff)
downloadsystemd-00adc340bb15bc9d634db6caa48f1c964b99f79a.tar.gz
inotify-util: declare iterator in FOREACH_INOTIFY_EVENT()
This also makes the macro check if the event is actually in the buffer, and if it is not, then log about that and finish the loop.
-rw-r--r--src/basic/inotify-util.h27
-rw-r--r--src/basic/terminal-util.c1
-rw-r--r--src/core/cgroup.c3
-rw-r--r--src/core/path.c3
-rw-r--r--src/libsystemd/sd-journal/sd-journal.c1
-rw-r--r--src/libsystemd/sd-network/sd-network.c1
-rw-r--r--src/timesync/wait-sync.c3
-rw-r--r--src/udev/udevd.c3
8 files changed, 27 insertions, 15 deletions
diff --git a/src/basic/inotify-util.h b/src/basic/inotify-util.h
index 88af08688f..eaf1922bab 100644
--- a/src/basic/inotify-util.h
+++ b/src/basic/inotify-util.h
@@ -6,12 +6,31 @@
#include <stddef.h>
#include <sys/inotify.h>
+#include "log.h"
+
#define INOTIFY_EVENT_MAX (offsetof(struct inotify_event, name) + NAME_MAX + 1)
-#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
- for ((e) = &buffer.ev; \
- (uint8_t*) (e) < (uint8_t*) (buffer.raw) + (sz); \
- (e) = (struct inotify_event*) ((uint8_t*) (e) + sizeof(struct inotify_event) + (e)->len))
+#define _FOREACH_INOTIFY_EVENT(e, buffer, sz, log_level, start, end) \
+ for (struct inotify_event \
+ *start = &((buffer).ev), \
+ *end = (struct inotify_event*) ((uint8_t*) start + (sz)), \
+ *e = start; \
+ (uint8_t*) e + sizeof(struct inotify_event) <= (uint8_t*) end && \
+ (uint8_t*) e + sizeof(struct inotify_event) + e->len <= (uint8_t*) end ? true : \
+ ({ \
+ log_full(log_level, "Received invalid inotify event, ignoring."); \
+ false; \
+ }); \
+ e = (struct inotify_event*) ((uint8_t*) e + sizeof(struct inotify_event) + e->len))
+
+#define _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, log_level) \
+ _FOREACH_INOTIFY_EVENT(e, buffer, sz, log_level, UNIQ_T(start, UNIQ), UNIQ_T(end, UNIQ))
+
+#define FOREACH_INOTIFY_EVENT(e, buffer, sz) \
+ _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, LOG_DEBUG)
+
+#define FOREACH_INOTIFY_EVENT_WARN(e, buffer, sz) \
+ _FOREACH_INOTIFY_EVENT_FULL(e, buffer, sz, LOG_WARNING)
union inotify_event_buffer {
struct inotify_event ev;
diff --git a/src/basic/terminal-util.c b/src/basic/terminal-util.c
index 79bb33df84..6119f21c1b 100644
--- a/src/basic/terminal-util.c
+++ b/src/basic/terminal-util.c
@@ -432,7 +432,6 @@ int acquire_terminal(
for (;;) {
union inotify_event_buffer buffer;
- struct inotify_event *e;
ssize_t l;
if (timeout != USEC_INFINITY) {
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
index 42055e4e41..cf10f31d37 100644
--- a/src/core/cgroup.c
+++ b/src/core/cgroup.c
@@ -3227,7 +3227,6 @@ static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents,
for (;;) {
union inotify_event_buffer buffer;
- struct inotify_event *e;
ssize_t l;
l = read(fd, &buffer, sizeof(buffer));
@@ -3238,7 +3237,7 @@ static int on_cgroup_inotify_event(sd_event_source *s, int fd, uint32_t revents,
return log_error_errno(errno, "Failed to read control group inotify events: %m");
}
- FOREACH_INOTIFY_EVENT(e, buffer, l) {
+ FOREACH_INOTIFY_EVENT_WARN(e, buffer, l) {
Unit *u;
if (e->wd < 0)
diff --git a/src/core/path.c b/src/core/path.c
index d9b136f10c..0814391222 100644
--- a/src/core/path.c
+++ b/src/core/path.c
@@ -173,7 +173,6 @@ void path_spec_unwatch(PathSpec *s) {
int path_spec_fd_event(PathSpec *s, uint32_t revents) {
union inotify_event_buffer buffer;
- struct inotify_event *e;
ssize_t l;
assert(s);
@@ -191,7 +190,7 @@ int path_spec_fd_event(PathSpec *s, uint32_t revents) {
}
if (IN_SET(s->type, PATH_CHANGED, PATH_MODIFIED))
- FOREACH_INOTIFY_EVENT(e, buffer, l)
+ FOREACH_INOTIFY_EVENT_WARN(e, buffer, l)
if (s->primary_wd == e->wd)
return 1;
diff --git a/src/libsystemd/sd-journal/sd-journal.c b/src/libsystemd/sd-journal/sd-journal.c
index 62755ed965..9a886711a9 100644
--- a/src/libsystemd/sd-journal/sd-journal.c
+++ b/src/libsystemd/sd-journal/sd-journal.c
@@ -2677,7 +2677,6 @@ _public_ int sd_journal_process(sd_journal *j) {
for (;;) {
union inotify_event_buffer buffer;
- struct inotify_event *e;
ssize_t l;
l = read(j->inotify_fd, &buffer, sizeof(buffer));
diff --git a/src/libsystemd/sd-network/sd-network.c b/src/libsystemd/sd-network/sd-network.c
index e1e9a399c1..a5612ab2d3 100644
--- a/src/libsystemd/sd-network/sd-network.c
+++ b/src/libsystemd/sd-network/sd-network.c
@@ -469,7 +469,6 @@ sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m) {
int sd_network_monitor_flush(sd_network_monitor *m) {
union inotify_event_buffer buffer;
- struct inotify_event *e;
ssize_t l;
int fd, k;
diff --git a/src/timesync/wait-sync.c b/src/timesync/wait-sync.c
index f42e6496bb..57e6cd3403 100644
--- a/src/timesync/wait-sync.c
+++ b/src/timesync/wait-sync.c
@@ -80,7 +80,6 @@ static int inotify_handler(sd_event_source *s,
sd_event *event = sd_event_source_get_event(s);
ClockState *sp = userdata;
union inotify_event_buffer buffer;
- struct inotify_event *e;
ssize_t l;
l = read(fd, &buffer, sizeof(buffer));
@@ -90,7 +89,7 @@ static int inotify_handler(sd_event_source *s,
return log_warning_errno(errno, "Lost access to inotify: %m");
}
- FOREACH_INOTIFY_EVENT(e, buffer, l)
+ FOREACH_INOTIFY_EVENT_WARN(e, buffer, l)
process_inotify_event(event, sp, e);
return 0;
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index 1e90b94f5c..108142e9c6 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -1334,7 +1334,6 @@ static int synthesize_change(sd_device *dev) {
static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
Manager *manager = userdata;
union inotify_event_buffer buffer;
- struct inotify_event *e;
ssize_t l;
int r;
@@ -1352,7 +1351,7 @@ static int on_inotify(sd_event_source *s, int fd, uint32_t revents, void *userda
return log_error_errno(errno, "Failed to read inotify fd: %m");
}
- FOREACH_INOTIFY_EVENT(e, buffer, l) {
+ FOREACH_INOTIFY_EVENT_WARN(e, buffer, l) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *devnode;