From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id C35275A0290 for ; Tue, 23 May 2023 04:25:49 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4QQJ8B1hzRz4x4B; Tue, 23 May 2023 12:25:45 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1684808746; bh=EZU+DG53d/QqyG9/l2Tp8rLPLyG5OIn+MrAEilzNYKw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CnVYVwu9Q5V6XqA/Nsp6dT74XxrAkcgYa+TI3WjN4rNh7aPI7o2Q4QQMzbR2ReQZN PlcCgENq+hYMUdP4Bd+x9Bwyv0+IgyAyw7JLJWjFgWmu2ELCdXwRlwiWHKaFqPw3AC yp83QCk+8dgapzpHlAOcJ89QEFP8Gqw8XYWnzMM0= From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 1/2] test/nstool: Provide useful error if given a path that's too long Date: Tue, 23 May 2023 12:25:42 +1000 Message-Id: <20230523022543.623950-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230523022543.623950-1-david@gibson.dropbear.id.au> References: <20230523022543.623950-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: ENT6RQBZBO6B7UB5JH6OI5ZKJ3UNRRIW X-Message-ID-Hash: ENT6RQBZBO6B7UB5JH6OI5ZKJ3UNRRIW X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Normal filesystem paths can be very long (PATH_MAX is around 8k), however Unix domain sockets can only use relatively short paths (UNIX_PATH_MAX is 108 on Linux). Currently nstool will simply truncate paths that are too long, leading to difficult to understand failures. Make such failures clearer, with an explicit error message if given a path that's too long. Signed-off-by: David Gibson --- test/nstool.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/nstool.c b/test/nstool.c index e6d7d37..bca9569 100644 --- a/test/nstool.c +++ b/test/nstool.c @@ -93,14 +93,22 @@ static void usage(void) " terminate.\n"); } +static void sockaddr_from_path(struct sockaddr_un *addr, const char *sockpath) +{ + if (strlen(sockpath) > UNIX_PATH_MAX) + die("\"%s\" is too long for Unix socket path (%zu > %d)", + sockpath, strlen(sockpath), UNIX_PATH_MAX); + + addr->sun_family = AF_UNIX; + strncpy(addr->sun_path, sockpath, UNIX_PATH_MAX); +} + static int connect_ctl(const char *sockpath, bool wait, struct holder_info *info, struct ucred *peercred) { int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNIX); - struct sockaddr_un addr = { - .sun_family = AF_UNIX, - }; + struct sockaddr_un addr; struct holder_info discard; ssize_t len; int rc; @@ -108,7 +116,7 @@ static int connect_ctl(const char *sockpath, bool wait, if (fd < 0) die("socket(): %s\n", strerror(errno)); - strncpy(addr.sun_path, sockpath, UNIX_PATH_MAX); + sockaddr_from_path(&addr, sockpath); do { rc = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); @@ -149,9 +157,7 @@ static int connect_ctl(const char *sockpath, bool wait, static void cmd_hold(int argc, char *argv[]) { int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNIX); - struct sockaddr_un addr = { - .sun_family = AF_UNIX, - }; + struct sockaddr_un addr; const char *sockpath = argv[1]; struct holder_info info; int rc; @@ -162,7 +168,7 @@ static void cmd_hold(int argc, char *argv[]) if (fd < 0) die("socket(): %s\n", strerror(errno)); - strncpy(addr.sun_path, sockpath, UNIX_PATH_MAX); + sockaddr_from_path(&addr, sockpath); rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); if (rc < 0) -- 2.40.1