Thread: [Accel-ppp-users] [PATCH 1/3] L2TP: Fix socket() error handling in l2tp_connect()
Status: Beta
Brought to you by:
xebd
From: Guillaume N. <g....@al...> - 2012-07-12 13:22:27
|
Check if the tunnel file descriptor has been successfully created. Explicitely check for negative values to detect socket() errors. Signed-off-by: Guillaume Nault <g....@al...> --- accel-pppd/ctrl/l2tp/l2tp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/accel-pppd/ctrl/l2tp/l2tp.c b/accel-pppd/ctrl/l2tp/l2tp.c index d8a98f8..5bba25e 100644 --- a/accel-pppd/ctrl/l2tp/l2tp.c +++ b/accel-pppd/ctrl/l2tp/l2tp.c @@ -384,7 +384,7 @@ static int l2tp_connect(struct l2tp_conn_t *conn) pppox_addr.pppol2tp.d_tunnel = conn->peer_tid; conn->tunnel_fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); - if (!conn->ppp.fd) { + if (conn->tunnel_fd < 0) { log_ppp_error("l2tp: socket(AF_PPPOX): %s\n", strerror(errno)); return -1; } @@ -392,13 +392,13 @@ static int l2tp_connect(struct l2tp_conn_t *conn) fcntl(conn->tunnel_fd, F_SETFD, fcntl(conn->tunnel_fd, F_GETFD) | FD_CLOEXEC); conn->ppp.fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); - if (!conn->ppp.fd) { + if (conn->ppp.fd < 0) { close(conn->tunnel_fd); conn->tunnel_fd = -1; log_ppp_error("l2tp: socket(AF_PPPOX): %s\n", strerror(errno)); return -1; } - + fcntl(conn->ppp.fd, F_SETFD, fcntl(conn->ppp.fd, F_GETFD) | FD_CLOEXEC); if (connect(conn->tunnel_fd, (struct sockaddr *)&pppox_addr, sizeof(pppox_addr)) < 0) { -- 1.7.10.4 |
From: Guillaume N. <g....@al...> - 2012-07-12 13:22:32
|
Centralise error management to ensure full cleanup upon failure. Signed-off-by: Guillaume Nault <g....@al...> --- accel-pppd/ctrl/l2tp/l2tp.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/accel-pppd/ctrl/l2tp/l2tp.c b/accel-pppd/ctrl/l2tp/l2tp.c index 5bba25e..469492d 100644 --- a/accel-pppd/ctrl/l2tp/l2tp.c +++ b/accel-pppd/ctrl/l2tp/l2tp.c @@ -386,24 +386,22 @@ static int l2tp_connect(struct l2tp_conn_t *conn) conn->tunnel_fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); if (conn->tunnel_fd < 0) { log_ppp_error("l2tp: socket(AF_PPPOX): %s\n", strerror(errno)); - return -1; + goto out_err; } fcntl(conn->tunnel_fd, F_SETFD, fcntl(conn->tunnel_fd, F_GETFD) | FD_CLOEXEC); conn->ppp.fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); if (conn->ppp.fd < 0) { - close(conn->tunnel_fd); - conn->tunnel_fd = -1; log_ppp_error("l2tp: socket(AF_PPPOX): %s\n", strerror(errno)); - return -1; + goto out_err; } fcntl(conn->ppp.fd, F_SETFD, fcntl(conn->ppp.fd, F_GETFD) | FD_CLOEXEC); if (connect(conn->tunnel_fd, (struct sockaddr *)&pppox_addr, sizeof(pppox_addr)) < 0) { log_ppp_error("l2tp: connect(tunnel): %s\n", strerror(errno)); - return -1; + goto out_err; } pppox_addr.pppol2tp.s_session = conn->sid; @@ -411,12 +409,12 @@ static int l2tp_connect(struct l2tp_conn_t *conn) if (connect(conn->ppp.fd, (struct sockaddr *)&pppox_addr, sizeof(pppox_addr)) < 0) { log_ppp_error("l2tp: connect(session): %s\n", strerror(errno)); - return -1; + goto out_err; } if (setsockopt(conn->ppp.fd, SOL_PPPOL2TP, PPPOL2TP_SO_LNSMODE, &arg, sizeof(arg))) { log_ppp_error("l2tp: setsockopt: %s\n", strerror(errno)); - return -1; + goto out_err; } conn->ppp.chan_name = _strdup(inet_ntoa(conn->addr.sin_addr)); @@ -424,14 +422,25 @@ static int l2tp_connect(struct l2tp_conn_t *conn) triton_event_fire(EV_CTRL_STARTED, &conn->ppp); if (establish_ppp(&conn->ppp)) - return -1; + goto out_err; __sync_sub_and_fetch(&stat_starting, 1); __sync_add_and_fetch(&stat_active, 1); conn->state = STATE_PPP; - + return 0; + +out_err: + if (conn->tunnel_fd >= 0) { + close(conn->tunnel_fd); + conn->tunnel_fd = -1; + } + if (conn->ppp.fd >= 0) { + close(conn->ppp.fd); + conn->ppp.fd = -1; + } + return -1; } static void l2tp_rtimeout(struct triton_timer_t *t) -- 1.7.10.4 |
From: Guillaume N. <g....@al...> - 2012-07-12 13:22:46
|
Add error detection to ensure the FD_CLOEXEC flag gets set for every new socket. Signed-off-by: Guillaume Nault <g....@al...> --- accel-pppd/ctrl/l2tp/l2tp.c | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/accel-pppd/ctrl/l2tp/l2tp.c b/accel-pppd/ctrl/l2tp/l2tp.c index 469492d..fa76048 100644 --- a/accel-pppd/ctrl/l2tp/l2tp.c +++ b/accel-pppd/ctrl/l2tp/l2tp.c @@ -374,6 +374,7 @@ static int l2tp_connect(struct l2tp_conn_t *conn) { struct sockaddr_pppol2tp pppox_addr; int arg = 1; + int flg; memset(&pppox_addr, 0, sizeof(pppox_addr)); pppox_addr.sa_family = AF_PPPOX; @@ -388,8 +389,17 @@ static int l2tp_connect(struct l2tp_conn_t *conn) log_ppp_error("l2tp: socket(AF_PPPOX): %s\n", strerror(errno)); goto out_err; } - - fcntl(conn->tunnel_fd, F_SETFD, fcntl(conn->tunnel_fd, F_GETFD) | FD_CLOEXEC); + + flg = fcntl(conn->tunnel_fd, F_GETFD); + if (flg < 0) { + log_ppp_error("l2tp: fcntl(F_GETFD): %s\n", strerror(errno)); + goto out_err; + } + flg = fcntl(conn->tunnel_fd, F_SETFD, flg | FD_CLOEXEC); + if (flg < 0) { + log_ppp_error("l2tp: fcntl(F_SETFD): %s\n", strerror(errno)); + goto out_err; + } conn->ppp.fd = socket(AF_PPPOX, SOCK_DGRAM, PX_PROTO_OL2TP); if (conn->ppp.fd < 0) { @@ -397,7 +407,16 @@ static int l2tp_connect(struct l2tp_conn_t *conn) goto out_err; } - fcntl(conn->ppp.fd, F_SETFD, fcntl(conn->ppp.fd, F_GETFD) | FD_CLOEXEC); + flg = fcntl(conn->ppp.fd, F_GETFD); + if (flg < 0) { + log_ppp_error("l2tp: fcntl(F_GETFD): %s\n", strerror(errno)); + goto out_err; + } + flg = fcntl(conn->ppp.fd, F_SETFD, flg | FD_CLOEXEC); + if (flg < 0) { + log_ppp_error("l2tp: fcntl(F_SETFD): %s\n", strerror(errno)); + goto out_err; + } if (connect(conn->tunnel_fd, (struct sockaddr *)&pppox_addr, sizeof(pppox_addr)) < 0) { log_ppp_error("l2tp: connect(tunnel): %s\n", strerror(errno)); @@ -418,7 +437,7 @@ static int l2tp_connect(struct l2tp_conn_t *conn) } conn->ppp.chan_name = _strdup(inet_ntoa(conn->addr.sin_addr)); - + triton_event_fire(EV_CTRL_STARTED, &conn->ppp); if (establish_ppp(&conn->ppp)) -- 1.7.10.4 |