*: use frr_elevate_privs() (1/2: coccinelle)

Signed-off-by: David Lamparter <equinox@diac24.net>
This commit is contained in:
David Lamparter 2018-08-10 18:36:43 +02:00 committed by Quentin Young
parent 6017c3a2e7
commit 01b9e3fd0d
27 changed files with 442 additions and 588 deletions

View File

@ -559,13 +559,11 @@ int bgp_connect(struct peer *peer)
zlog_debug("Peer address not learnt: Returning from connect"); zlog_debug("Peer address not learnt: Returning from connect");
return 0; return 0;
} }
if (bgpd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&bgpd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
/* Make socket for the peer. */ /* Make socket for the peer. */
peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id, peer->fd = vrf_sockunion_socket(&peer->su, peer->bgp->vrf_id,
bgp_get_bound_name(peer)); bgp_get_bound_name(peer));
if (bgpd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (peer->fd < 0) if (peer->fd < 0)
return -1; return -1;
@ -584,16 +582,12 @@ int bgp_connect(struct peer *peer)
peer->host, safe_strerror(errno)); peer->host, safe_strerror(errno));
#ifdef IPTOS_PREC_INTERNETCONTROL #ifdef IPTOS_PREC_INTERNETCONTROL
if (bgpd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&bgpd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "%s: could not raise privs",
__func__);
if (sockunion_family(&peer->su) == AF_INET) if (sockunion_family(&peer->su) == AF_INET)
setsockopt_ipv4_tos(peer->fd, IPTOS_PREC_INTERNETCONTROL); setsockopt_ipv4_tos(peer->fd, IPTOS_PREC_INTERNETCONTROL);
else if (sockunion_family(&peer->su) == AF_INET6) else if (sockunion_family(&peer->su) == AF_INET6)
setsockopt_ipv6_tclass(peer->fd, IPTOS_PREC_INTERNETCONTROL); setsockopt_ipv6_tclass(peer->fd, IPTOS_PREC_INTERNETCONTROL);
if (bgpd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "%s: could not lower privs",
__func__);
#endif #endif
if (peer->password) if (peer->password)
@ -661,9 +655,7 @@ static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen,
sockopt_reuseaddr(sock); sockopt_reuseaddr(sock);
sockopt_reuseport(sock); sockopt_reuseport(sock);
if (bgpd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&bgpd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "%s: could not raise privs",
__func__);
#ifdef IPTOS_PREC_INTERNETCONTROL #ifdef IPTOS_PREC_INTERNETCONTROL
if (sa->sa_family == AF_INET) if (sa->sa_family == AF_INET)
@ -676,9 +668,7 @@ static int bgp_listener(int sock, struct sockaddr *sa, socklen_t salen,
ret = bind(sock, sa, salen); ret = bind(sock, sa, salen);
en = errno; en = errno;
if (bgpd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "%s: could not lower privs",
__func__);
if (ret < 0) { if (ret < 0) {
flog_err_sys(LIB_ERR_SOCKET, "bind: %s", safe_strerror(en)); flog_err_sys(LIB_ERR_SOCKET, "bind: %s", safe_strerror(en));
@ -724,12 +714,10 @@ int bgp_socket(struct bgp *bgp, unsigned short port, const char *address)
snprintf(port_str, sizeof(port_str), "%d", port); snprintf(port_str, sizeof(port_str), "%d", port);
port_str[sizeof(port_str) - 1] = '\0'; port_str[sizeof(port_str) - 1] = '\0';
if (bgpd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&bgpd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
ret = vrf_getaddrinfo(address, port_str, &req, &ainfo_save, ret = vrf_getaddrinfo(address, port_str, &req, &ainfo_save,
bgp->vrf_id); bgp->vrf_id);
if (bgpd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (ret != 0) { if (ret != 0) {
flog_err_sys(LIB_ERR_SOCKET, "getaddrinfo: %s", flog_err_sys(LIB_ERR_SOCKET, "getaddrinfo: %s",
gai_strerror(ret)); gai_strerror(ret));
@ -743,14 +731,13 @@ int bgp_socket(struct bgp *bgp, unsigned short port, const char *address)
if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6) if (ainfo->ai_family != AF_INET && ainfo->ai_family != AF_INET6)
continue; continue;
if (bgpd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&bgpd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges"); sock = vrf_socket(ainfo->ai_family,
sock = vrf_socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_socktype,
ainfo->ai_protocol, bgp->vrf_id, ainfo->ai_protocol, bgp->vrf_id,
(bgp->inst_type == BGP_INSTANCE_TYPE_VRF ? (bgp->inst_type == BGP_INSTANCE_TYPE_VRF ?
bgp->name : NULL)); bgp->name : NULL));
if (bgpd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (sock < 0) { if (sock < 0) {
flog_err_sys(LIB_ERR_SOCKET, "socket: %s", flog_err_sys(LIB_ERR_SOCKET, "socket: %s",
safe_strerror(errno)); safe_strerror(errno));

View File

@ -131,9 +131,7 @@ void eigrp_adjust_sndbuflen(struct eigrp *eigrp, unsigned int buflen)
/* Check if any work has to be done at all. */ /* Check if any work has to be done at all. */
if (eigrp->maxsndbuflen >= buflen) if (eigrp->maxsndbuflen >= buflen)
return; return;
if (eigrpd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&eigrpd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "%s: could not raise privs, %s",
__func__, safe_strerror(errno));
/* Now we try to set SO_SNDBUF to what our caller has requested /* Now we try to set SO_SNDBUF to what our caller has requested
* (the MTU of a newly added interface). However, if the OS has * (the MTU of a newly added interface). However, if the OS has
@ -151,9 +149,7 @@ void eigrp_adjust_sndbuflen(struct eigrp *eigrp, unsigned int buflen)
eigrp->maxsndbuflen = (unsigned int)newbuflen; eigrp->maxsndbuflen = (unsigned int)newbuflen;
else else
zlog_warn("%s: failed to get SO_SNDBUF", __func__); zlog_warn("%s: failed to get SO_SNDBUF", __func__);
if (eigrpd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "%s: could not lower privs, %s",
__func__, safe_strerror(errno));
} }
int eigrp_if_ipmulticast(struct eigrp *top, struct prefix *p, int eigrp_if_ipmulticast(struct eigrp *top, struct prefix *p,

View File

@ -187,15 +187,13 @@ int isis_sock_init(struct isis_circuit *circuit)
{ {
int retval = ISIS_OK; int retval = ISIS_OK;
if (isisd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&isisd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "%s: could not raise privs, %s",
__func__, safe_strerror(errno));
retval = open_bpf_dev(circuit); retval = open_bpf_dev(circuit);
if (retval != ISIS_OK) { if (retval != ISIS_OK) {
zlog_warn("%s: could not initialize the socket", __func__); zlog_warn("%s: could not initialize the socket", __func__);
goto end; break;
} }
if (if_is_broadcast(circuit->interface)) { if (if_is_broadcast(circuit->interface)) {
@ -204,13 +202,10 @@ int isis_sock_init(struct isis_circuit *circuit)
} else { } else {
zlog_warn("isis_sock_init(): unknown circuit type"); zlog_warn("isis_sock_init(): unknown circuit type");
retval = ISIS_WARNING; retval = ISIS_WARNING;
goto end; break;
} }
end: }
if (isisd_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "%s: could not lower privs, %s",
__func__, safe_strerror(errno));
return retval; return retval;
} }

View File

@ -468,15 +468,13 @@ int isis_sock_init(struct isis_circuit *circuit)
{ {
int retval = ISIS_OK; int retval = ISIS_OK;
if (isisd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&isisd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "%s: could not raise privs, %s",
__func__, safe_strerror(errno));
retval = open_dlpi_dev(circuit); retval = open_dlpi_dev(circuit);
if (retval != ISIS_OK) { if (retval != ISIS_OK) {
zlog_warn("%s: could not initialize the socket", __func__); zlog_warn("%s: could not initialize the socket", __func__);
goto end; break;
} }
if (circuit->circ_type == CIRCUIT_T_BROADCAST) { if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
@ -485,13 +483,10 @@ int isis_sock_init(struct isis_circuit *circuit)
} else { } else {
zlog_warn("isis_sock_init(): unknown circuit type"); zlog_warn("isis_sock_init(): unknown circuit type");
retval = ISIS_WARNING; retval = ISIS_WARNING;
goto end; break;
} }
end: }
if (isisd_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "%s: could not lower privs, %s",
__func__, safe_strerror(errno));
return retval; return retval;
} }

View File

@ -185,15 +185,13 @@ int isis_sock_init(struct isis_circuit *circuit)
{ {
int retval = ISIS_OK; int retval = ISIS_OK;
if (isisd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&isisd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "%s: could not raise privs, %s",
__func__, safe_strerror(errno));
retval = open_packet_socket(circuit); retval = open_packet_socket(circuit);
if (retval != ISIS_OK) { if (retval != ISIS_OK) {
zlog_warn("%s: could not initialize the socket", __func__); zlog_warn("%s: could not initialize the socket", __func__);
goto end; break;
} }
/* Assign Rx and Tx callbacks are based on real if type */ /* Assign Rx and Tx callbacks are based on real if type */
@ -206,13 +204,10 @@ int isis_sock_init(struct isis_circuit *circuit)
} else { } else {
zlog_warn("isis_sock_init(): unknown circuit type"); zlog_warn("isis_sock_init(): unknown circuit type");
retval = ISIS_WARNING; retval = ISIS_WARNING;
goto end; break;
} }
end: }
if (isisd_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "%s: could not lower privs, %s",
__func__, safe_strerror(errno));
return retval; return retval;
} }

View File

@ -262,17 +262,13 @@ int
sock_set_bindany(int fd, int enable) sock_set_bindany(int fd, int enable)
{ {
#ifdef HAVE_SO_BINDANY #ifdef HAVE_SO_BINDANY
if (ldpd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&ldpd_privs) {
log_warn("%s: could not raise privs", __func__);
if (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &enable, if (setsockopt(fd, SOL_SOCKET, SO_BINDANY, &enable,
sizeof(int)) < 0) { sizeof(int)) < 0) {
if (ldpd_privs.change(ZPRIVS_LOWER))
log_warn("%s: could not lower privs", __func__);
log_warn("%s: error setting SO_BINDANY", __func__); log_warn("%s: error setting SO_BINDANY", __func__);
return (-1); return (-1);
} }
if (ldpd_privs.change(ZPRIVS_LOWER)) }
log_warn("%s: could not lower privs", __func__);
return (0); return (0);
#elif defined(HAVE_IP_FREEBIND) #elif defined(HAVE_IP_FREEBIND)
if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &enable, sizeof(int)) < 0) { if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &enable, sizeof(int)) < 0) {

View File

@ -76,21 +76,14 @@ static void ospf6_set_checksum(void)
/* Make ospf6d's server socket. */ /* Make ospf6d's server socket. */
int ospf6_serv_sock(void) int ospf6_serv_sock(void)
{ {
if (ospf6d_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&ospf6d_privs) {
flog_err(LIB_ERR_PRIVILEGES,
"ospf6_serv_sock: could not raise privs");
ospf6_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP); ospf6_sock = socket(AF_INET6, SOCK_RAW, IPPROTO_OSPFIGP);
if (ospf6_sock < 0) { if (ospf6_sock < 0) {
zlog_warn("Network: can't create OSPF6 socket."); zlog_warn("Network: can't create OSPF6 socket.");
if (ospf6d_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"ospf6_sock_init: could not lower privs");
return -1; return -1;
} }
if (ospf6d_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"ospf6_sock_init: could not lower privs");
/* set socket options */ /* set socket options */
#if 1 #if 1

View File

@ -56,14 +56,11 @@ static int pim_mroute_set(struct pim_instance *pim, int enable)
* We need to create the VRF table for the pim mroute_socket * We need to create the VRF table for the pim mroute_socket
*/ */
if (pim->vrf_id != VRF_DEFAULT) { if (pim->vrf_id != VRF_DEFAULT) {
if (pimd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&pimd_privs) {
flog_err(
LIB_ERR_PRIVILEGES,
"pim_mroute_socket_enable: could not raise privs, %s",
safe_strerror(errno));
opt = pim->vrf->data.l.table_id; opt = pim->vrf->data.l.table_id;
err = setsockopt(pim->mroute_socket, IPPROTO_IP, MRT_TABLE, err = setsockopt(pim->mroute_socket, IPPROTO_IP,
MRT_TABLE,
&opt, opt_len); &opt, opt_len);
if (err) { if (err) {
zlog_warn( zlog_warn(
@ -74,11 +71,7 @@ static int pim_mroute_set(struct pim_instance *pim, int enable)
return -1; return -1;
} }
if (pimd_privs.change(ZPRIVS_LOWER)) }
flog_err(
LIB_ERR_PRIVILEGES,
"pim_mroute_socket_enable: could not lower privs, %s",
safe_strerror(errno));
} }
opt = enable ? MRT_INIT : MRT_DONE; opt = enable ? MRT_INIT : MRT_DONE;
@ -711,15 +704,13 @@ int pim_mroute_socket_enable(struct pim_instance *pim)
{ {
int fd; int fd;
if (pimd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&pimd_privs) {
flog_err(LIB_ERR_PRIVILEGES,
"pim_mroute_socket_enable: could not raise privs, %s",
safe_strerror(errno));
fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP); fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP);
if (fd < 0) { if (fd < 0) {
zlog_warn("Could not create mroute socket: errno=%d: %s", errno, zlog_warn("Could not create mroute socket: errno=%d: %s",
errno,
safe_strerror(errno)); safe_strerror(errno));
return -2; return -2;
} }
@ -735,10 +726,7 @@ int pim_mroute_socket_enable(struct pim_instance *pim)
} }
#endif #endif
if (pimd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"pim_mroute_socket_enable: could not lower privs, %s",
safe_strerror(errno));
pim->mroute_socket = fd; pim->mroute_socket = fd;
if (pim_mroute_set(pim, 1)) { if (pim_mroute_set(pim, 1)) {

View File

@ -46,17 +46,11 @@ int pim_socket_raw(int protocol)
{ {
int fd; int fd;
if (pimd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&pimd_privs) {
flog_err(LIB_ERR_PRIVILEGES,
"pim_sockek_raw: could not raise privs, %s",
safe_strerror(errno));
fd = socket(AF_INET, SOCK_RAW, protocol); fd = socket(AF_INET, SOCK_RAW, protocol);
if (pimd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"pim_socket_raw: could not lower privs, %s",
safe_strerror(errno));
if (fd < 0) { if (fd < 0) {
zlog_warn("Could not create raw socket: errno=%d: %s", errno, zlog_warn("Could not create raw socket: errno=%d: %s", errno,
@ -71,18 +65,13 @@ void pim_socket_ip_hdr(int fd)
{ {
const int on = 1; const int on = 1;
if (pimd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&pimd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "%s: could not raise privs, %s",
__PRETTY_FUNCTION__, safe_strerror(errno));
if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on))) if (setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)))
flog_err_sys(LIB_ERR_SOCKET, zlog_err("%s: Could not turn on IP_HDRINCL option: %s",
"%s: Could not turn on IP_HDRINCL option: %s",
__PRETTY_FUNCTION__, safe_strerror(errno)); __PRETTY_FUNCTION__, safe_strerror(errno));
if (pimd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "%s: could not lower privs, %s",
__PRETTY_FUNCTION__, safe_strerror(errno));
} }
/* /*
@ -94,16 +83,12 @@ int pim_socket_bind(int fd, struct interface *ifp)
int ret = 0; int ret = 0;
#ifdef SO_BINDTODEVICE #ifdef SO_BINDTODEVICE
if (pimd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&pimd_privs) {
flog_err(LIB_ERR_PRIVILEGES, "%s: could not raise privs, %s",
__PRETTY_FUNCTION__, safe_strerror(errno));
ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifp->name, ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, ifp->name,
strlen(ifp->name)); strlen(ifp->name));
if (pimd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "%s: could not lower privs, %s",
__PRETTY_FUNCTION__, safe_strerror(errno));
#endif #endif
return ret; return ret;

View File

@ -1357,31 +1357,23 @@ static int rip_create_socket(void)
setsockopt_ipv4_tos(sock, IPTOS_PREC_INTERNETCONTROL); setsockopt_ipv4_tos(sock, IPTOS_PREC_INTERNETCONTROL);
#endif #endif
if (ripd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&ripd_privs) {
flog_err(LIB_ERR_PRIVILEGES,
"rip_create_socket: could not raise privs");
setsockopt_so_recvbuf(sock, RIP_UDP_RCV_BUF); setsockopt_so_recvbuf(sock, RIP_UDP_RCV_BUF);
if ((ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr))) < 0) if ((ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr))) < 0)
{ {
int save_errno = errno; int save_errno = errno;
if (ripd_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"rip_create_socket: could not lower privs");
flog_err_sys(LIB_ERR_SOCKET, zlog_err("%s: Can't bind socket %d to %s port %d: %s",
"%s: Can't bind socket %d to %s port %d: %s", __func__,
__func__, sock, inet_ntoa(addr.sin_addr), sock, inet_ntoa(addr.sin_addr),
(int)ntohs(addr.sin_port), (int)ntohs(addr.sin_port), safe_strerror(save_errno));
safe_strerror(save_errno));
close(sock); close(sock);
return ret; return ret;
} }
if (ripd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"rip_create_socket: could not lower privs");
return sock; return sock;
} }

View File

@ -72,19 +72,14 @@ static int ripng_multicast_join(struct interface *ifp)
* While this is bogus, privs are available and easy to use * While this is bogus, privs are available and easy to use
* for this call as a workaround. * for this call as a workaround.
*/ */
if (ripngd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&ripngd_privs) {
flog_err(
LIB_ERR_PRIVILEGES,
"ripng_multicast_join: could not raise privs");
ret = setsockopt(ripng->sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, ret = setsockopt(ripng->sock, IPPROTO_IPV6,
IPV6_JOIN_GROUP,
(char *)&mreq, sizeof(mreq)); (char *)&mreq, sizeof(mreq));
save_errno = errno; save_errno = errno;
if (ripngd_privs.change(ZPRIVS_LOWER)) }
flog_err(
LIB_ERR_PRIVILEGES,
"ripng_multicast_join: could not lower privs");
if (ret < 0 && save_errno == EADDRINUSE) { if (ret < 0 && save_errno == EADDRINUSE) {
/* /*

View File

@ -125,22 +125,14 @@ static int ripng_make_socket(void)
#endif /* SIN6_LEN */ #endif /* SIN6_LEN */
ripaddr.sin6_port = htons(RIPNG_PORT_DEFAULT); ripaddr.sin6_port = htons(RIPNG_PORT_DEFAULT);
if (ripngd_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&ripngd_privs) {
flog_err(LIB_ERR_PRIVILEGES,
"ripng_make_socket: could not raise privs");
ret = bind(sock, (struct sockaddr *)&ripaddr, sizeof(ripaddr)); ret = bind(sock, (struct sockaddr *)&ripaddr, sizeof(ripaddr));
if (ret < 0) { if (ret < 0) {
flog_err_sys(LIB_ERR_SOCKET, "Can't bind ripng socket: %s.", zlog_err("Can't bind ripng socket: %s.", safe_strerror(errno));
safe_strerror(errno));
if (ripngd_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"ripng_make_socket: could not lower privs");
goto error; goto error;
} }
if (ripngd_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"ripng_make_socket: could not lower privs");
return sock; return sock;
error: error:

76
tools/zprivs.cocci Normal file
View File

@ -0,0 +1,76 @@
@@
identifier change;
identifier end;
expression E, f, g;
iterator name frr_elevate_privs;
@@
- if (E.change(ZPRIVS_RAISE))
- f;
+ frr_elevate_privs(&E) {
<+...
- goto end;
+ break;
...+>
- end:
- if (E.change(ZPRIVS_LOWER))
- g;
+ }
@@
identifier change, errno, safe_strerror, exit;
expression E, f1, f2, f3, ret, fn;
iterator name frr_elevate_privs;
@@
if (E.change(ZPRIVS_RAISE))
f1;
...
if (...) {
- int save_errno = errno;
...
- if (E.change(ZPRIVS_LOWER))
- f2;
...
- safe_strerror(save_errno)
+ safe_strerror(errno)
...
\( return ret; \| exit(ret); \)
}
...
if (E.change(ZPRIVS_LOWER))
f3;
@@
identifier change;
expression E, f1, f2, f3, ret;
iterator name frr_elevate_privs;
@@
if (E.change(ZPRIVS_RAISE))
f1;
...
if (...) {
...
- if (E.change(ZPRIVS_LOWER))
- f2;
...
return ret;
}
...
if (E.change(ZPRIVS_LOWER))
f3;
@@
identifier change;
expression E, f, g;
iterator name frr_elevate_privs;
@@
- if (E.change(ZPRIVS_RAISE))
- f;
+ frr_elevate_privs(&E) {
...
- if (E.change(ZPRIVS_LOWER))
- g;
+ }

View File

@ -59,8 +59,7 @@ static int interface_list_ioctl(int af)
size_t needed, lastneeded = 0; size_t needed, lastneeded = 0;
char *buf = NULL; char *buf = NULL;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
sock = socket(af, SOCK_DGRAM, 0); sock = socket(af, SOCK_DGRAM, 0);
if (sock < 0) { if (sock < 0) {
@ -68,9 +67,6 @@ static int interface_list_ioctl(int af)
(af == AF_INET ? "AF_INET" : "AF_INET6"), (af == AF_INET ? "AF_INET" : "AF_INET6"),
safe_strerror(errno)); safe_strerror(errno));
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
return -1; return -1;
} }
@ -80,8 +76,7 @@ calculate_lifc_len: /* must hold privileges to enter here */
ret = ioctl(sock, SIOCGLIFNUM, &lifn); ret = ioctl(sock, SIOCGLIFNUM, &lifn);
save_errno = errno; save_errno = errno;
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (ret < 0) { if (ret < 0) {
zlog_warn("interface_list_ioctl: SIOCGLIFNUM failed %s", zlog_warn("interface_list_ioctl: SIOCGLIFNUM failed %s",

View File

@ -375,9 +375,9 @@ static int get_iflink_speed(struct interface *interface)
ifdata.ifr_data = (caddr_t)&ecmd; ifdata.ifr_data = (caddr_t)&ecmd;
/* use ioctl to get IP address of an interface */ /* use ioctl to get IP address of an interface */
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges"); sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP,
sd = vrf_socket(PF_INET, SOCK_DGRAM, IPPROTO_IP, interface->vrf_id, interface->vrf_id,
NULL); NULL);
if (sd < 0) { if (sd < 0) {
if (IS_ZEBRA_DEBUG_KERNEL) if (IS_ZEBRA_DEBUG_KERNEL)
@ -387,8 +387,7 @@ static int get_iflink_speed(struct interface *interface)
} }
/* Get the current link state for the interface */ /* Get the current link state for the interface */
rc = vrf_ioctl(interface->vrf_id, sd, SIOCETHTOOL, (char *)&ifdata); rc = vrf_ioctl(interface->vrf_id, sd, SIOCETHTOOL, (char *)&ifdata);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (rc < 0) { if (rc < 0) {
if (IS_ZEBRA_DEBUG_KERNEL) if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug( zlog_debug(

View File

@ -55,22 +55,16 @@ int if_ioctl(unsigned long request, caddr_t buffer)
int ret; int ret;
int err = 0; int err = 0;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
sock = socket(AF_INET, SOCK_DGRAM, 0); sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) { if (sock < 0) {
int save_errno = errno; zlog_err("Cannot create UDP socket: %s",
safe_strerror(errno));
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
flog_err_sys(LIB_ERR_SOCKET, "Cannot create UDP socket: %s",
safe_strerror(save_errno));
exit(1); exit(1);
} }
if ((ret = ioctl(sock, request, buffer)) < 0) if ((ret = ioctl(sock, request, buffer)) < 0)
err = errno; err = errno;
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
close(sock); close(sock);
if (ret < 0) { if (ret < 0) {
@ -87,23 +81,17 @@ int vrf_if_ioctl(unsigned long request, caddr_t buffer, vrf_id_t vrf_id)
int ret; int ret;
int err = 0; int err = 0;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
sock = vrf_socket(AF_INET, SOCK_DGRAM, 0, vrf_id, NULL); sock = vrf_socket(AF_INET, SOCK_DGRAM, 0, vrf_id, NULL);
if (sock < 0) { if (sock < 0) {
int save_errno = errno; zlog_err("Cannot create UDP socket: %s",
safe_strerror(errno));
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
flog_err_sys(LIB_ERR_SOCKET, "Cannot create UDP socket: %s",
safe_strerror(save_errno));
exit(1); exit(1);
} }
ret = vrf_ioctl(vrf_id, sock, request, buffer); ret = vrf_ioctl(vrf_id, sock, request, buffer);
if (ret < 0) if (ret < 0)
err = errno; err = errno;
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
close(sock); close(sock);
if (ret < 0) { if (ret < 0) {
@ -120,24 +108,17 @@ static int if_ioctl_ipv6(unsigned long request, caddr_t buffer)
int ret; int ret;
int err = 0; int err = 0;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
sock = socket(AF_INET6, SOCK_DGRAM, 0); sock = socket(AF_INET6, SOCK_DGRAM, 0);
if (sock < 0) { if (sock < 0) {
int save_errno = errno; zlog_err("Cannot create IPv6 datagram socket: %s",
safe_strerror(errno));
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
flog_err_sys(LIB_ERR_SOCKET,
"Cannot create IPv6 datagram socket: %s",
safe_strerror(save_errno));
exit(1); exit(1);
} }
if ((ret = ioctl(sock, request, buffer)) < 0) if ((ret = ioctl(sock, request, buffer)) < 0)
err = errno; err = errno;
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
close(sock); close(sock);
if (ret < 0) { if (ret < 0) {

View File

@ -58,24 +58,19 @@ int if_ioctl(unsigned long request, caddr_t buffer)
int ret; int ret;
int err; int err;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
sock = socket(AF_INET, SOCK_DGRAM, 0); sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) { if (sock < 0) {
int save_errno = errno; zlog_err("Cannot create UDP socket: %s",
if (zserv_privs.change(ZPRIVS_LOWER)) safe_strerror(errno));
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
flog_err_sys(LIB_ERR_SOCKET, "Cannot create UDP socket: %s",
safe_strerror(save_errno));
exit(1); exit(1);
} }
if ((ret = ioctl(sock, request, buffer)) < 0) if ((ret = ioctl(sock, request, buffer)) < 0)
err = errno; err = errno;
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
close(sock); close(sock);
@ -93,25 +88,19 @@ int if_ioctl_ipv6(unsigned long request, caddr_t buffer)
int ret; int ret;
int err; int err;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
sock = socket(AF_INET6, SOCK_DGRAM, 0); sock = socket(AF_INET6, SOCK_DGRAM, 0);
if (sock < 0) { if (sock < 0) {
int save_errno = errno; zlog_err("Cannot create IPv6 datagram socket: %s",
if (zserv_privs.change(ZPRIVS_LOWER)) safe_strerror(errno));
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
flog_err_sys(LIB_ERR_SOCKET,
"Cannot create IPv6 datagram socket: %s",
safe_strerror(save_errno));
exit(1); exit(1);
} }
if ((ret = ioctl(sock, request, buffer)) < 0) if ((ret = ioctl(sock, request, buffer)) < 0)
err = errno; err = errno;
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
close(sock); close(sock);

View File

@ -77,17 +77,11 @@ int ipforward_on(void)
{ {
FILE *fp; FILE *fp;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges, %s",
safe_strerror(errno));
fp = fopen(proc_ipv4_forwarding, "w"); fp = fopen(proc_ipv4_forwarding, "w");
if (fp == NULL) { if (fp == NULL) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"Can't lower privileges, %s",
safe_strerror(errno));
return -1; return -1;
} }
@ -95,9 +89,7 @@ int ipforward_on(void)
fclose(fp); fclose(fp);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges, %s",
safe_strerror(errno));
return ipforward(); return ipforward();
} }
@ -106,17 +98,11 @@ int ipforward_off(void)
{ {
FILE *fp; FILE *fp;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges, %s",
safe_strerror(errno));
fp = fopen(proc_ipv4_forwarding, "w"); fp = fopen(proc_ipv4_forwarding, "w");
if (fp == NULL) { if (fp == NULL) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"Can't lower privileges, %s",
safe_strerror(errno));
return -1; return -1;
} }
@ -124,9 +110,7 @@ int ipforward_off(void)
fclose(fp); fclose(fp);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges, %s",
safe_strerror(errno));
return ipforward(); return ipforward();
} }
@ -160,17 +144,11 @@ int ipforward_ipv6_on(void)
{ {
FILE *fp; FILE *fp;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges, %s",
safe_strerror(errno));
fp = fopen(proc_ipv6_forwarding, "w"); fp = fopen(proc_ipv6_forwarding, "w");
if (fp == NULL) { if (fp == NULL) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"Can't lower privileges, %s",
safe_strerror(errno));
return -1; return -1;
} }
@ -178,9 +156,7 @@ int ipforward_ipv6_on(void)
fclose(fp); fclose(fp);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges, %s",
safe_strerror(errno));
return ipforward_ipv6(); return ipforward_ipv6();
} }
@ -190,17 +166,11 @@ int ipforward_ipv6_off(void)
{ {
FILE *fp; FILE *fp;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges, %s",
safe_strerror(errno));
fp = fopen(proc_ipv6_forwarding, "w"); fp = fopen(proc_ipv6_forwarding, "w");
if (fp == NULL) { if (fp == NULL) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"Can't lower privileges, %s",
safe_strerror(errno));
return -1; return -1;
} }
@ -208,9 +178,7 @@ int ipforward_ipv6_off(void)
fclose(fp); fclose(fp);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges, %s",
safe_strerror(errno));
return ipforward_ipv6(); return ipforward_ipv6();
} }

View File

@ -82,31 +82,21 @@ static int solaris_nd(const int cmd, const char *parameter, const int value)
strioctl.ic_len = ND_BUFFER_SIZE; strioctl.ic_len = ND_BUFFER_SIZE;
strioctl.ic_dp = nd_buf; strioctl.ic_dp = nd_buf;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES,
"solaris_nd: Can't raise privileges");
if ((fd = open(device, O_RDWR)) < 0) { if ((fd = open(device, O_RDWR)) < 0) {
zlog_warn("failed to open device %s - %s", device, zlog_warn("failed to open device %s - %s", device,
safe_strerror(errno)); safe_strerror(errno));
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"solaris_nd: Can't lower privileges");
return -1; return -1;
} }
if (ioctl(fd, I_STR, &strioctl) < 0) { if (ioctl(fd, I_STR, &strioctl) < 0) {
int save_errno = errno;
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES,
"solaris_nd: Can't lower privileges");
close(fd); close(fd);
zlog_warn("ioctl I_STR failed on device %s - %s", device, zlog_warn("ioctl I_STR failed on device %s - %s",
safe_strerror(save_errno)); device,
safe_strerror(errno));
return -1; return -1;
} }
close(fd); close(fd);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"solaris_nd: Can't lower privileges");
if (cmd == ND_GET) { if (cmd == ND_GET) {
errno = 0; errno = 0;

View File

@ -54,16 +54,12 @@ int ipforward_on(void)
int ipforwarding = 1; int ipforwarding = 1;
len = sizeof ipforwarding; len = sizeof ipforwarding;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
if (sysctl(mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0) { if (sysctl(mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
zlog_warn("Can't set ipforwarding on"); zlog_warn("Can't set ipforwarding on");
return -1; return -1;
} }
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
return ipforwarding; return ipforwarding;
} }
@ -73,16 +69,12 @@ int ipforward_off(void)
int ipforwarding = 0; int ipforwarding = 0;
len = sizeof ipforwarding; len = sizeof ipforwarding;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
if (sysctl(mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0) { if (sysctl(mib, MIB_SIZ, NULL, NULL, &ipforwarding, len) < 0) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
zlog_warn("Can't set ipforwarding on"); zlog_warn("Can't set ipforwarding on");
return -1; return -1;
} }
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
return ipforwarding; return ipforwarding;
} }
@ -101,16 +93,12 @@ int ipforward_ipv6(void)
int ip6forwarding = 0; int ip6forwarding = 0;
len = sizeof ip6forwarding; len = sizeof ip6forwarding;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
if (sysctl(mib_ipv6, MIB_SIZ, &ip6forwarding, &len, 0, 0) < 0) { if (sysctl(mib_ipv6, MIB_SIZ, &ip6forwarding, &len, 0, 0) < 0) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
zlog_warn("can't get ip6forwarding value"); zlog_warn("can't get ip6forwarding value");
return -1; return -1;
} }
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
return ip6forwarding; return ip6forwarding;
} }
@ -120,16 +108,12 @@ int ipforward_ipv6_on(void)
int ip6forwarding = 1; int ip6forwarding = 1;
len = sizeof ip6forwarding; len = sizeof ip6forwarding;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
if (sysctl(mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0) { if (sysctl(mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
zlog_warn("can't get ip6forwarding value"); zlog_warn("can't get ip6forwarding value");
return -1; return -1;
} }
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
return ip6forwarding; return ip6forwarding;
} }
@ -139,16 +123,12 @@ int ipforward_ipv6_off(void)
int ip6forwarding = 0; int ip6forwarding = 0;
len = sizeof ip6forwarding; len = sizeof ip6forwarding;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
if (sysctl(mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0) { if (sysctl(mib_ipv6, MIB_SIZ, NULL, NULL, &ip6forwarding, len) < 0) {
if (zserv_privs.change(ZPRIVS_LOWER))
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
zlog_warn("can't get ip6forwarding value"); zlog_warn("can't get ip6forwarding value");
return -1; return -1;
} }
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
return ip6forwarding; return ip6forwarding;
} }

View File

@ -81,18 +81,12 @@ int irdp_sock_init(void)
int save_errno; int save_errno;
int sock; int sock;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES,
"irdp_sock_init: could not raise privs, %s",
safe_strerror(errno));
sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
save_errno = errno; save_errno = errno;
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"irdp_sock_init: could not lower privs, %s",
safe_strerror(errno));
if (sock < 0) { if (sock < 0) {
zlog_warn("IRDP: can't create irdp socket %s", zlog_warn("IRDP: can't create irdp socket %s",

View File

@ -170,14 +170,11 @@ static int netlink_recvbuf(struct nlsock *nl, uint32_t newsize)
} }
/* Try force option (linux >= 2.6.14) and fall back to normal set */ /* Try force option (linux >= 2.6.14) and fall back to normal set */
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE,
"routing_socket: Can't raise privileges"); &nl_rcvbufsize,
ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUFFORCE, &nl_rcvbufsize,
sizeof(nl_rcvbufsize)); sizeof(nl_rcvbufsize));
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"routing_socket: Can't lower privileges");
if (ret < 0) if (ret < 0)
ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF, ret = setsockopt(nl->sock, SOL_SOCKET, SO_RCVBUF,
&nl_rcvbufsize, sizeof(nl_rcvbufsize)); &nl_rcvbufsize, sizeof(nl_rcvbufsize));
@ -957,12 +954,10 @@ int netlink_talk(int (*filter)(struct nlmsghdr *, ns_id_t, int startup),
n->nlmsg_flags); n->nlmsg_flags);
/* Send message to netlink interface. */ /* Send message to netlink interface. */
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
status = sendmsg(nl->sock, &msg, 0); status = sendmsg(nl->sock, &msg, 0);
save_errno = errno; save_errno = errno;
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) { if (IS_ZEBRA_DEBUG_KERNEL_MSGDUMP_SEND) {
zlog_debug("%s: >> netlink message dump [sent]", __func__); zlog_debug("%s: >> netlink message dump [sent]", __func__);

View File

@ -403,8 +403,7 @@ enum dp_req_result kernel_route_rib(struct route_node *rn,
return DP_REQUEST_FAILURE; return DP_REQUEST_FAILURE;
} }
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
if (old) if (old)
route |= kernel_rtm(RTM_DELETE, p, old); route |= kernel_rtm(RTM_DELETE, p, old);
@ -412,8 +411,7 @@ enum dp_req_result kernel_route_rib(struct route_node *rn,
if (new) if (new)
route |= kernel_rtm(RTM_ADD, p, new); route |= kernel_rtm(RTM_ADD, p, new);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (new) { if (new) {
kernel_route_rib_pass_fail( kernel_route_rib_pass_fail(

View File

@ -630,17 +630,11 @@ static int rtadv_make_socket(ns_id_t ns_id)
int ret = 0; int ret = 0;
struct icmp6_filter filter; struct icmp6_filter filter;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES,
"rtadv_make_socket: could not raise privs, %s",
safe_strerror(errno));
sock = ns_socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, ns_id); sock = ns_socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, ns_id);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES,
"rtadv_make_socket: could not lower privs, %s",
safe_strerror(errno));
if (sock < 0) { if (sock < 0) {
return -1; return -1;

View File

@ -117,11 +117,9 @@ static int kernel_send_rtmsg_v4(int action, mpls_label_t in_label,
hdr.rtm_mpls = MPLS_OP_SWAP; hdr.rtm_mpls = MPLS_OP_SWAP;
} }
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
ret = writev(kr_state.fd, iov, iovcnt); ret = writev(kr_state.fd, iov, iovcnt);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (ret == -1) if (ret == -1)
flog_err_sys(LIB_ERR_SOCKET, "%s: %s", __func__, flog_err_sys(LIB_ERR_SOCKET, "%s: %s", __func__,
@ -226,11 +224,9 @@ static int kernel_send_rtmsg_v6(int action, mpls_label_t in_label,
hdr.rtm_mpls = MPLS_OP_SWAP; hdr.rtm_mpls = MPLS_OP_SWAP;
} }
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
ret = writev(kr_state.fd, iov, iovcnt); ret = writev(kr_state.fd, iov, iovcnt);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (ret == -1) if (ret == -1)
flog_err_sys(LIB_ERR_SOCKET, "%s: %s", __func__, flog_err_sys(LIB_ERR_SOCKET, "%s: %s", __func__,

View File

@ -76,11 +76,9 @@ static void zebra_ns_notify_create_context_from_entry_name(const char *name)
if (netnspath == NULL) if (netnspath == NULL)
return; return;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
ns_id = zebra_ns_id_get(netnspath); ns_id = zebra_ns_id_get(netnspath);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (ns_id == NS_UNKNOWN) if (ns_id == NS_UNKNOWN)
return; return;
ns_id_external = ns_map_nsid_with_external(ns_id, true); ns_id_external = ns_map_nsid_with_external(ns_id, true);
@ -97,12 +95,10 @@ static void zebra_ns_notify_create_context_from_entry_name(const char *name)
ns_map_nsid_with_external(ns_id, false); ns_map_nsid_with_external(ns_id, false);
return; return;
} }
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
ret = vrf_netns_handler_create(NULL, vrf, netnspath, ret = vrf_netns_handler_create(NULL, vrf, netnspath,
ns_id_external, ns_id); ns_id_external, ns_id);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (ret != CMD_SUCCESS) { if (ret != CMD_SUCCESS) {
zlog_warn("NS notify : failed to create NS %s", netnspath); zlog_warn("NS notify : failed to create NS %s", netnspath);
ns_map_nsid_with_external(ns_id, false); ns_map_nsid_with_external(ns_id, false);
@ -169,20 +165,16 @@ static int zebra_ns_ready_read(struct thread *t)
netnspath = zns_info->netnspath; netnspath = zns_info->netnspath;
if (--zns_info->retries == 0) if (--zns_info->retries == 0)
stop_retry = 1; stop_retry = 1;
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
err = ns_switch_to_netns(netnspath); err = ns_switch_to_netns(netnspath);
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (err < 0) if (err < 0)
return zebra_ns_continue_read(zns_info, stop_retry); return zebra_ns_continue_read(zns_info, stop_retry);
/* go back to default ns */ /* go back to default ns */
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
err = ns_switchback_to_initial(); err = ns_switchback_to_initial();
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
if (err < 0) if (err < 0)
return zebra_ns_continue_read(zns_info, stop_retry); return zebra_ns_continue_read(zns_info, stop_retry);

View File

@ -315,11 +315,9 @@ int zebra_ns_init(void)
dzns = zebra_ns_alloc(); dzns = zebra_ns_alloc();
if (zserv_privs.change(ZPRIVS_RAISE)) frr_elevate_privs(&zserv_privs) {
flog_err(LIB_ERR_PRIVILEGES, "Can't raise privileges");
ns_id = zebra_ns_id_get_default(); ns_id = zebra_ns_id_get_default();
if (zserv_privs.change(ZPRIVS_LOWER)) }
flog_err(LIB_ERR_PRIVILEGES, "Can't lower privileges");
ns_id_external = ns_map_nsid_with_external(ns_id, true); ns_id_external = ns_map_nsid_with_external(ns_id, true);
ns_init_management(ns_id_external, ns_id); ns_init_management(ns_id_external, ns_id);