diff --git a/src/basic/alloc-util.h b/src/basic/alloc-util.h index ebe42889ea..62b591117b 100644 --- a/src/basic/alloc-util.h +++ b/src/basic/alloc-util.h @@ -52,8 +52,11 @@ static inline void freep(void *p) { #define _cleanup_free_ _cleanup_(freep) +/* Checks the size arguments of allocation functions for overflow in multiplication. In addition, checks if either of + * them is 0; that is almost certainly an error (e.g., an overflow in computing _need_), so it's better to fail (and + * we cannot leave this check to malloc, because the behavior of malloc(0) is impl. specific). */ static inline bool size_multiply_overflow(size_t size, size_t need) { - return _unlikely_(need != 0 && size > (SIZE_MAX / need)); + return _unlikely_(need == 0 || size == 0 || size > (SIZE_MAX / need)); } _malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) { diff --git a/src/boot/efi/console.h b/src/boot/efi/console.h index 10c5ce4ebd..b9ed6c70b3 100644 --- a/src/boot/efi/console.h +++ b/src/boot/efi/console.h @@ -9,7 +9,7 @@ #define EFI_CONTROL_PRESSED (EFI_RIGHT_CONTROL_PRESSED|EFI_LEFT_CONTROL_PRESSED) #define EFI_ALT_PRESSED (EFI_RIGHT_ALT_PRESSED|EFI_LEFT_ALT_PRESSED) -#define KEYPRESS(keys, scan, uni) ((((UINT64)keys) << 32) | ((scan) << 16) | (uni)) +#define KEYPRESS(keys, scan, uni) ((((UINT64)keys) << 32) | (((UINT64)scan) << 16) | (uni)) #define KEYCHAR(k) ((k) & 0xffff) #define CHAR_CTRL(c) ((c) - 'a' + 1) diff --git a/src/core/manager.c b/src/core/manager.c index 6e29ddde43..af84a0a769 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -892,7 +892,7 @@ static int manager_setup_notify(Manager *m) { (void) mkdir_parents_label(m->notify_socket, 0755); (void) unlink(m->notify_socket); - strncpy(sa.un.sun_path, m->notify_socket, sizeof(sa.un.sun_path)-1); + strncpy(sa.un.sun_path, m->notify_socket, sizeof(sa.un.sun_path)); r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); if (r < 0) return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index e9dddbc0d7..9c92d67516 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -1846,6 +1846,9 @@ static int journal_file_append_entry_internal( void journal_file_post_change(JournalFile *f) { assert(f); + if (f->fd < 0) + return; + /* inotify() does not receive IN_MODIFY events from file * accesses done via mmap(). After each access we hence * trigger IN_MODIFY by truncating the journal file to its diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 8e22538ac3..ed6da4445b 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -2157,6 +2157,7 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd if (cancelled && m->enable_wall_messages) { _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; + _cleanup_free_ char *username = NULL; const char *tty = NULL; uid_t uid = 0; int r; @@ -2167,8 +2168,9 @@ static int method_cancel_scheduled_shutdown(sd_bus_message *message, void *userd (void) sd_bus_creds_get_tty(creds, &tty); } + username = uid_to_name(uid); utmp_wall("The system shutdown has been cancelled", - uid_to_name(uid), tty, logind_wall_tty_filter, m); + username, tty, logind_wall_tty_filter, m); } return sd_bus_reply_method_return(message, "b", cancelled); diff --git a/src/login/logind-utmp.c b/src/login/logind-utmp.c index 71ebdfcfb1..8bdd4ab6bf 100644 --- a/src/login/logind-utmp.c +++ b/src/login/logind-utmp.c @@ -61,7 +61,7 @@ bool logind_wall_tty_filter(const char *tty, void *userdata) { static int warn_wall(Manager *m, usec_t n) { char date[FORMAT_TIMESTAMP_MAX] = {}; - _cleanup_free_ char *l = NULL; + _cleanup_free_ char *l = NULL, *username = NULL; usec_t left; int r; @@ -83,8 +83,8 @@ static int warn_wall(Manager *m, usec_t n) { return 0; } - utmp_wall(l, uid_to_name(m->scheduled_shutdown_uid), - m->scheduled_shutdown_tty, logind_wall_tty_filter, m); + username = uid_to_name(m->scheduled_shutdown_uid); + utmp_wall(l, username, m->scheduled_shutdown_tty, logind_wall_tty_filter, m); return 1; } diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c index 8464df122c..90ccbd7a83 100644 --- a/src/login/pam_systemd.c +++ b/src/login/pam_systemd.c @@ -160,7 +160,7 @@ static int get_seat_from_display(const char *display, const char **seat, uint32_ r = socket_from_display(display, &p); if (r < 0) return r; - strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)-1); + strncpy(sa.un.sun_path, p, sizeof(sa.un.sun_path)); fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); if (fd < 0) diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 27547051c8..134c4c3b5d 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -2826,7 +2826,7 @@ static int setup_sd_notify_child(void) { (void) mkdir_parents(NSPAWN_NOTIFY_SOCKET_PATH, 0755); (void) unlink(NSPAWN_NOTIFY_SOCKET_PATH); - strncpy(sa.un.sun_path, NSPAWN_NOTIFY_SOCKET_PATH, sizeof(sa.un.sun_path)-1); + strncpy(sa.un.sun_path, NSPAWN_NOTIFY_SOCKET_PATH, sizeof(sa.un.sun_path)); r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); if (r < 0) { safe_close(fd); diff --git a/src/shared/dissect-image.c b/src/shared/dissect-image.c index b6e0534b5d..710507ed5c 100644 --- a/src/shared/dissect-image.c +++ b/src/shared/dissect-image.c @@ -231,7 +231,7 @@ int dissect_image( .node = TAKE_PTR(n), }; - m->encrypted = streq(fstype, "crypto_LUKS"); + m->encrypted = streq_ptr(fstype, "crypto_LUKS"); *ret = TAKE_PTR(m); diff --git a/src/shared/efivars.c b/src/shared/efivars.c index 3931bee559..da70e68c81 100644 --- a/src/shared/efivars.c +++ b/src/shared/efivars.c @@ -412,9 +412,13 @@ int efi_get_boot_option( if (header->path_len > 0) { uint8_t *dbuf; - size_t dnext; + size_t dnext, doff; + + doff = offsetof(struct boot_option, title) + title_size; + dbuf = buf + doff; + if (header->path_len > l - doff) + return -EINVAL; - dbuf = buf + offsetof(struct boot_option, title) + title_size; dnext = 0; while (dnext < header->path_len) { struct device_path *dpath; diff --git a/src/shared/fdset.c b/src/shared/fdset.c index 8c852f1137..5d277328c7 100644 --- a/src/shared/fdset.c +++ b/src/shared/fdset.c @@ -211,13 +211,16 @@ fail: int fdset_close_others(FDSet *fds) { void *e; Iterator i; - int *a; + int *a = NULL; size_t j = 0, m; m = fdset_size(fds); - a = newa(int, m); - SET_FOREACH(e, MAKE_SET(fds), i) - a[j++] = PTR_TO_FD(e); + + if (m > 0) { + a = newa(int, m); + SET_FOREACH(e, MAKE_SET(fds), i) + a[j++] = PTR_TO_FD(e); + } assert(j == m); diff --git a/src/shared/firewall-util.c b/src/shared/firewall-util.c index eb4f5ff616..cba52fb419 100644 --- a/src/shared/firewall-util.c +++ b/src/shared/firewall-util.c @@ -50,8 +50,14 @@ static int entry_fill_basics( entry->ip.proto = protocol; if (in_interface) { + size_t l; + + l = strlen(in_interface); + assert(l < sizeof entry->ip.iniface); + assert(l < sizeof entry->ip.iniface_mask); + strcpy(entry->ip.iniface, in_interface); - memset(entry->ip.iniface_mask, 0xFF, strlen(in_interface)+1); + memset(entry->ip.iniface_mask, 0xFF, l + 1); } if (source) { entry->ip.src = source->in;