From patchwork Thu Aug 16 19:55:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Terry Wilson X-Patchwork-Id: 958584 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41rxrj1TrSz9s3x for ; Fri, 17 Aug 2018 05:55:16 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 4525DD56; Thu, 16 Aug 2018 19:55:14 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 6CF38D3B for ; Thu, 16 Aug 2018 19:55:12 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 15977762 for ; Thu, 16 Aug 2018 19:55:12 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8E63C30BB530 for ; Thu, 16 Aug 2018 19:55:11 +0000 (UTC) Received: from test.rdocloud (ovpn-112-36.phx2.redhat.com [10.3.112.36]) by smtp.corp.redhat.com (Postfix) with ESMTP id 59ED15D9C5; Thu, 16 Aug 2018 19:55:11 +0000 (UTC) From: Terry Wilson To: dev@openvswitch.org Date: Thu, 16 Aug 2018 19:55:09 +0000 Message-Id: <1534449309-21642-1-git-send-email-twilson@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Thu, 16 Aug 2018 19:55:11 +0000 (UTC) X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH] Fix socket permissions on Linux X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Unix sockets were not being created with the permission 0770, instead using the current umask value. The manpage for fchmod() states that that if filedes refers to a socket, the behavior is undefined. Insetad, use the same code as *BSD to ensure the 0770 permission is set on unix sockets. Signed-off-by: Terry Wilson --- lib/socket-util-unix.c | 56 +++++++++++++++++++------------------------------- 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/lib/socket-util-unix.c b/lib/socket-util-unix.c index 59f63fc..54147d1 100644 --- a/lib/socket-util-unix.c +++ b/lib/socket-util-unix.c @@ -263,42 +263,28 @@ static int bind_unix_socket(int fd, struct sockaddr *sun, socklen_t sun_len) { const mode_t mode = 0770; /* Allow both user and group access. */ - if (LINUX) { - /* On Linux, the fd's permissions become the file's permissions. - * fchmod() does not affect other files, like umask() does. */ - if (fchmod(fd, mode)) { - return errno; - } - - /* Must be after fchmod(). */ - if (bind(fd, sun, sun_len)) { - return errno; - } - return 0; + /* On unix sockets, only the umask affects permissions. The + * umask is process-wide rather than thread-specific, so we have to use + * a subprocess for safety. */ + pid_t pid = fork(); + + if (!pid) { + umask(mode ^ 0777); + _exit(bind(fd, sun, sun_len) ? errno : 0); + } else if (pid > 0) { + int status; + int error; + + do { + error = waitpid(pid, &status, 0) < 0 ? errno : 0; + } while (error == EINTR); + + return (error ? error + : WIFEXITED(status) ? WEXITSTATUS(status) + : WIFSIGNALED(status) ? EINTR + : ECHILD /* WTF? */); } else { - /* On FreeBSD and NetBSD, only the umask affects permissions. The - * umask is process-wide rather than thread-specific, so we have to use - * a subprocess for safety. */ - pid_t pid = fork(); - - if (!pid) { - umask(mode ^ 0777); - _exit(bind(fd, sun, sun_len) ? errno : 0); - } else if (pid > 0) { - int status; - int error; - - do { - error = waitpid(pid, &status, 0) < 0 ? errno : 0; - } while (error == EINTR); - - return (error ? error - : WIFEXITED(status) ? WEXITSTATUS(status) - : WIFSIGNALED(status) ? EINTR - : ECHILD /* WTF? */); - } else { - return errno; - } + return errno; } }