Skip to content

Commit c69f902

Browse files
committed
register_listener: don't exit on failure, just return false
Found by the ZeroPath AI Security Engineer <https://zeropath.com>
1 parent b040db2 commit c69f902

File tree

1 file changed

+42
-17
lines changed

1 file changed

+42
-17
lines changed

logsrvd/logsrvd.c

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,11 +1602,11 @@ new_connection(int sock, bool tls, const union sockaddr_union *sa_un,
16021602
}
16031603

16041604
static int
1605-
create_listener(struct server_address *addr)
1605+
open_listener_socket(struct server_address *addr)
16061606
{
16071607
int flags, on, sock;
16081608
const char *family = "inet4";
1609-
debug_decl(create_listener, SUDO_DEBUG_UTIL);
1609+
debug_decl(open_listener_socket, SUDO_DEBUG_UTIL);
16101610

16111611
if ((sock = socket(addr->sa_un.sa.sa_family, SOCK_STREAM, 0)) == -1) {
16121612
sudo_warn("socket");
@@ -1649,6 +1649,22 @@ create_listener(struct server_address *addr)
16491649
debug_return_int(-1);
16501650
}
16511651

1652+
static void
1653+
free_listener(struct listener *l)
1654+
{
1655+
debug_decl(free_listener, SUDO_DEBUG_UTIL);
1656+
1657+
if (l != NULL) {
1658+
sudo_ev_free(l->ev);
1659+
sudo_rcstr_delref(l->sa_str);
1660+
if (l->sock != -1)
1661+
close(l->sock);
1662+
free(l);
1663+
}
1664+
1665+
debug_return;
1666+
}
1667+
16521668
static void
16531669
listener_cb(int fd, int what, void *v)
16541670
{
@@ -1687,29 +1703,41 @@ listener_cb(int fd, int what, void *v)
16871703
static bool
16881704
register_listener(struct server_address *addr, struct sudo_event_base *evbase)
16891705
{
1690-
struct listener *l;
1706+
struct listener *l = NULL;
16911707
int sock;
16921708
debug_decl(register_listener, SUDO_DEBUG_UTIL);
16931709

1694-
sock = create_listener(addr);
1710+
sock = open_listener_socket(addr);
16951711
if (sock == -1)
1696-
debug_return_bool(false);
1712+
goto bad;
16971713

1698-
/* TODO: make non-fatal */
1699-
if ((l = malloc(sizeof(*l))) == NULL)
1700-
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1714+
if ((l = calloc(1, sizeof(*l))) == NULL) {
1715+
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1716+
goto bad;
1717+
}
1718+
l->ev = sudo_ev_alloc(sock, SUDO_EV_READ|SUDO_EV_PERSIST, listener_cb, l);
1719+
if (l->ev == NULL) {
1720+
sudo_warnx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1721+
goto bad;
1722+
}
17011723
l->sa_str = addr->sa_str;
17021724
sudo_rcstr_addref(l->sa_str);
17031725
l->sock = sock;
1726+
sock = -1;
17041727
l->tls = addr->tls;
1705-
l->ev = sudo_ev_alloc(sock, SUDO_EV_READ|SUDO_EV_PERSIST, listener_cb, l);
1706-
if (l->ev == NULL)
1707-
sudo_fatalx(U_("%s: %s"), __func__, U_("unable to allocate memory"));
1708-
if (sudo_ev_add(evbase, l->ev, NULL, false) == -1)
1709-
sudo_fatal("%s", U_("unable to add event to queue"));
1728+
1729+
if (sudo_ev_add(evbase, l->ev, NULL, false) == -1) {
1730+
sudo_warn("%s", U_("unable to add event to queue"));
1731+
goto bad;
1732+
}
17101733
TAILQ_INSERT_TAIL(&listeners, l, entries);
17111734

17121735
debug_return_bool(true);
1736+
bad:
1737+
if (sock != -1)
1738+
close(sock);
1739+
free_listener(l);
1740+
debug_return_bool(false);
17131741
}
17141742

17151743
/*
@@ -1741,10 +1769,7 @@ server_setup(struct sudo_event_base *base)
17411769
}
17421770
if (addr == NULL) {
17431771
/* Listener not used in new config. */
1744-
sudo_rcstr_delref(l->sa_str);
1745-
sudo_ev_free(l->ev);
1746-
close(l->sock);
1747-
free(l);
1772+
free_listener(l);
17481773
}
17491774
}
17501775

0 commit comments

Comments
 (0)