From patchwork Thu Aug 8 19:40:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yann Droneaud X-Patchwork-Id: 2841361 Return-Path: X-Original-To: patchwork-linux-rdma@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B77FBBF546 for ; Thu, 8 Aug 2013 19:53:13 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6070F20362 for ; Thu, 8 Aug 2013 19:53:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 37F7420365 for ; Thu, 8 Aug 2013 19:53:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966252Ab3HHTw6 (ORCPT ); Thu, 8 Aug 2013 15:52:58 -0400 Received: from smtp22.services.sfr.fr ([93.17.128.13]:15158 "EHLO smtp22.services.sfr.fr" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S966236Ab3HHTw4 (ORCPT ); Thu, 8 Aug 2013 15:52:56 -0400 Received: from filter.sfr.fr (localhost [127.0.0.1]) by msfrf2201.sfr.fr (SMTP Server) with ESMTP id B590F70000A3; Thu, 8 Aug 2013 21:42:48 +0200 (CEST) Received: from localhost.localdomain (187.20.90.92.rev.sfr.net [92.90.20.187]) by msfrf2201.sfr.fr (SMTP Server) with ESMTP id 2972070000AF; Thu, 8 Aug 2013 21:42:48 +0200 (CEST) X-SFR-UUID: 20130808194248169.2972070000AF@msfrf2201.sfr.fr Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by localhost.localdomain (8.14.7/8.14.7) with ESMTP id r78JgfR0003067; Thu, 8 Aug 2013 21:42:47 +0200 Received: (from ydroneaud@localhost) by localhost.localdomain (8.14.7/8.14.7/Submit) id r78JgQ8D003066; Thu, 8 Aug 2013 21:42:26 +0200 From: Yann Droneaud To: linux-rdma@vger.kernel.org Cc: Yann Droneaud Subject: [PATCH libibverbs v2 05/11] read_config_file(): use the directory file descriptor to open configuration file Date: Thu, 8 Aug 2013 21:40:48 +0200 Message-Id: X-Mailer: git-send-email 1.8.3.1 In-Reply-To: References: In-Reply-To: References: MIME-Version: 1.0 Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Change read_config_file() to use the directory file descriptor when - checking the configuration file with fstatat()[1][2], - opening the configuration file with openat()[3][4]. The full path string is no more needed so it's removed. Additionally, using the directory fd and openat() ensure the file opened is in the expected directory. Weakness addressed: - CWE-363: Race Condition Enabling Link Following Compatibility note: - According to Gnulib[5], fstatat() is not available on older systems. - According to Gnulib[6], openat() is not available on older systems. Links: - [1] fstatat - [2] fstatat(2) - [3] openat - [4] openat(2) Signed-off-by: Yann Droneaud --- src/init.c | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/init.c b/src/init.c index 953ed9f..c260628 100644 --- a/src/init.c +++ b/src/init.c @@ -235,8 +235,9 @@ static void load_drivers(void) } } -static void read_config_file(const char *path) +static void read_config_file(int conf_dirfd, const char *name) { + int fd; FILE *conf; char *line = NULL; char *config; @@ -245,25 +246,32 @@ static void read_config_file(const char *path) ssize_t len; struct stat buf; - if (stat(path, &buf)) { - fprintf(stderr, PFX "Warning: couldn't stat config file '%s'.\n", - path); + if (fstatat(conf_dirfd, name, &buf, 0)) { + fprintf(stderr, PFX "Warning: couldn't stat config file '%s/%s'.\n", + IBV_CONFIG_DIR, name); return; } if (!S_ISREG(buf.st_mode)) { - fprintf(stderr, PFX "Warning: invalid config file '%s'.\n", - path); + fprintf(stderr, PFX "Warning: invalid config file '%s/%s'.\n", + IBV_CONFIG_DIR, name); return; } - conf = fopen(path, "r" STREAM_CLOEXEC); - if (!conf) { - fprintf(stderr, PFX "Warning: couldn't read config file %s.\n", - path); + fd = openat(conf_dirfd, name, O_RDONLY | O_CLOEXEC); + if (fd == -1) { + fprintf(stderr, PFX "Warning: couldn't read config file '%s/%s'.\n", + IBV_CONFIG_DIR, name); return; } + conf = fdopen(fd, "r" STREAM_CLOEXEC); + if (!conf) { + fprintf(stderr, PFX "Warning: couldn't read config file '%s/%s'.\n", + IBV_CONFIG_DIR, name); + goto out; + } + while ((len = getline(&line, &buflen, conf)) != -1) { config = line + strspn(line, "\t "); if (config[0] == '\n' || config[0] == '#') @@ -296,12 +304,18 @@ static void read_config_file(const char *path) driver_name_list = driver_name; } else fprintf(stderr, PFX "Warning: ignoring bad config directive " - "'%s' in file '%s'.\n", field, path); + "'%s' in file '%s/%s'.\n", field, IBV_CONFIG_DIR, name); } if (line) free(line); + fclose(conf); + + return; + +out: + close(fd); } static void read_config(void) @@ -309,7 +323,6 @@ static void read_config(void) int conf_dirfd; DIR *conf_dir; struct dirent *dent; - char *path; conf_dirfd = open(IBV_CONFIG_DIR, O_RDONLY | O_CLOEXEC); if (conf_dirfd == -1) { @@ -334,18 +347,9 @@ static void read_config(void) if (dent->d_name[0] == '\0' || dent->d_name[strlen(dent->d_name) - 1] == '~') continue; - if (asprintf(&path, "%s/%s", IBV_CONFIG_DIR, dent->d_name) < 0) { - fprintf(stderr, PFX "Warning: couldn't read config file %s/%s.\n", - IBV_CONFIG_DIR, dent->d_name); - goto out; - } - - read_config_file(path); - - free(path); + read_config_file(conf_dirfd, dent->d_name); } -out: closedir(conf_dir); }