From patchwork Mon Dec 10 21:29:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Michal_Such=C3=A1nek?= X-Patchwork-Id: 10722563 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CF2A61751 for ; Mon, 10 Dec 2018 21:29:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BE2582A276 for ; Mon, 10 Dec 2018 21:29:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AF9452A243; Mon, 10 Dec 2018 21:29:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 527D52A243 for ; Mon, 10 Dec 2018 21:29:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727034AbeLJV3v (ORCPT ); Mon, 10 Dec 2018 16:29:51 -0500 Received: from mx2.suse.de ([195.135.220.15]:35786 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727818AbeLJV3v (ORCPT ); Mon, 10 Dec 2018 16:29:51 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 2F834AECF for ; Mon, 10 Dec 2018 21:29:50 +0000 (UTC) From: Michal Suchanek To: linux-modules@vger.kernel.org Cc: Michal Suchanek Subject: [PATCH v2 1/3] depmod: prevent module dependency files missing during depmod invocation. Date: Mon, 10 Dec 2018 22:29:32 +0100 Message-Id: X-Mailer: git-send-email 2.19.2 In-Reply-To: References: MIME-Version: 1.0 Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Depmod deletes the module dependency files before moving the temporary files in their place. This results in user seeing no dependency files while they are updated. Remove the unlink call. The rename call should suffice to move the new file in place and unlink the old one. It should also do both atomically so there is no window when no dependency file exists. Signed-off-by: Michal Suchanek --- tools/depmod.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/depmod.c b/tools/depmod.c index 989d9077926c..18c0d61b2db3 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -2451,7 +2451,6 @@ static int depmod_output(struct depmod *depmod, FILE *out) break; } - unlinkat(dfd, itr->name, 0); if (renameat(dfd, tmp, dfd, itr->name) != 0) { err = -errno; CRIT("renameat(%s, %s, %s, %s): %m\n", From patchwork Mon Dec 10 21:29:33 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Michal_Such=C3=A1nek?= X-Patchwork-Id: 10722565 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1255D91E for ; Mon, 10 Dec 2018 21:29:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 03B762A243 for ; Mon, 10 Dec 2018 21:29:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EBED42A276; Mon, 10 Dec 2018 21:29:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8430B2A263 for ; Mon, 10 Dec 2018 21:29:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727818AbeLJV3w (ORCPT ); Mon, 10 Dec 2018 16:29:52 -0500 Received: from mx2.suse.de ([195.135.220.15]:35792 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728059AbeLJV3w (ORCPT ); Mon, 10 Dec 2018 16:29:52 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 61B12AEF3 for ; Mon, 10 Dec 2018 21:29:50 +0000 (UTC) From: Michal Suchanek To: linux-modules@vger.kernel.org Cc: Michal Suchanek Subject: [PATCH v2 2/3] depmod: prevent module dependency files corruption due to parallel invocation. Date: Mon, 10 Dec 2018 22:29:33 +0100 Message-Id: <636a25c106ac3da29803025248a237e9d23f4e9d.1544476531.git.msuchanek@suse.de> X-Mailer: git-send-email 2.19.2 In-Reply-To: References: MIME-Version: 1.0 Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Depmod does not use unique filename for temporary files. There is no guarantee the user does not attempt to run mutiple depmod processes in parallel. If that happens a temporary file might be created by depmod(1st), truncated by depmod(2nd), and renamed to final name by depmod(1st) resulting in corrupted file seen by user. Due to missing mkstempat() this is more complex than it should be. Adding PID and timestamp to the filename should be reasonably reliable. Adding O_EXCL as mkstemp does fails creating the file rather than corrupting existing file. Signed-off-by: Michal Suchanek --- tools/depmod.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/depmod.c b/tools/depmod.c index 18c0d61b2db3..3b6d16e76160 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -2398,6 +2399,9 @@ static int depmod_output(struct depmod *depmod, FILE *out) }; const char *dname = depmod->cfg->dirname; int dfd, err = 0; + struct timeval tv; + + gettimeofday(&tv, NULL); if (out != NULL) dfd = -1; @@ -2412,15 +2416,17 @@ static int depmod_output(struct depmod *depmod, FILE *out) for (itr = depfiles; itr->name != NULL; itr++) { FILE *fp = out; - char tmp[NAME_MAX] = ""; + char tmp[NAME_MAX + 1] = ""; int r, ferr; if (fp == NULL) { - int flags = O_CREAT | O_TRUNC | O_WRONLY; + int flags = O_CREAT | O_TRUNC | O_WRONLY | O_EXCL; int mode = 0644; int fd; - snprintf(tmp, sizeof(tmp), "%s.tmp", itr->name); + snprintf(tmp, sizeof(tmp), "%s.%i.%li.%li", itr->name, getpid(), + tv.tv_usec, tv.tv_sec); + tmp[NAME_MAX] = 0; fd = openat(dfd, tmp, flags, mode); if (fd < 0) { ERR("openat(%s, %s, %o, %o): %m\n", From patchwork Mon Dec 10 21:29:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Michal_Such=C3=A1nek?= X-Patchwork-Id: 10722567 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3EF4F3E9D for ; Mon, 10 Dec 2018 21:29:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2EE612A243 for ; Mon, 10 Dec 2018 21:29:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 234042A263; Mon, 10 Dec 2018 21:29:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AA1452A296 for ; Mon, 10 Dec 2018 21:29:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728059AbeLJV3w (ORCPT ); Mon, 10 Dec 2018 16:29:52 -0500 Received: from mx2.suse.de ([195.135.220.15]:35798 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728088AbeLJV3w (ORCPT ); Mon, 10 Dec 2018 16:29:52 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 951F3AF59 for ; Mon, 10 Dec 2018 21:29:50 +0000 (UTC) From: Michal Suchanek To: linux-modules@vger.kernel.org Cc: Michal Suchanek Subject: [PATCH v2 3/3] depmod: shut up gcc insufficinet buffer warning. Date: Mon, 10 Dec 2018 22:29:34 +0100 Message-Id: X-Mailer: git-send-email 2.19.2 In-Reply-To: References: MIME-Version: 1.0 Sender: owner-linux-modules@vger.kernel.org Precedence: bulk List-ID: X-Virus-Scanned: ClamAV using ClamSMTP In a couple of places depmod concatenates the module directory and filename with snprintf. This can technically overflow creating an unterminated string if module directory name is long. Use openat instead as is done elsewhere in depmod. This avoids the snprintf, the extra buffer on stack, and the gcc warning. It may even fix a corner case when the module direcotry name is just under PATH_MAX. Signed-off-by: Michal Suchanek --- tools/depmod.c | 51 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/tools/depmod.c b/tools/depmod.c index 3b6d16e76160..fb76f823c016 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -1389,19 +1389,42 @@ static int depmod_modules_build_array(struct depmod *depmod) return 0; } +static FILE * dfdopen(const char * dname, const char * filename, int flags, const char * mode) + { + int fd, dfd; + FILE * ret; + + dfd = open(dname, O_RDONLY); + if (dfd < 0) { + WRN("could not open directory %s: %m\n", dname); + return NULL; + } + + fd = openat(dfd, filename, flags); + if (fd < 0) { + WRN("could not open %s at %s: %m\n", filename, dname); + ret = NULL; + } else { + ret = fdopen(fd, mode); + if (!ret) + WRN("could not associate stream with %s: %m\n", filename); + } + close(dfd); + return ret; +} + + + static void depmod_modules_sort(struct depmod *depmod) { - char order_file[PATH_MAX], line[PATH_MAX]; + char line[PATH_MAX]; + const char * order_file = "modules.order"; FILE *fp; unsigned idx = 0, total = 0; - snprintf(order_file, sizeof(order_file), "%s/modules.order", - depmod->cfg->dirname); - fp = fopen(order_file, "r"); - if (fp == NULL) { - WRN("could not open %s: %m\n", order_file); + fp = dfdopen(depmod->cfg->dirname, order_file, O_RDONLY, "r"); + if (fp == NULL) return; - } while (fgets(line, sizeof(line), fp) != NULL) { size_t len = strlen(line); @@ -1409,8 +1432,8 @@ static void depmod_modules_sort(struct depmod *depmod) if (len == 0) continue; if (line[len - 1] != '\n') { - ERR("%s:%u corrupted line misses '\\n'\n", - order_file, idx); + ERR("%s/%s:%u corrupted line misses '\\n'\n", + depmod->cfg->dirname, order_file, idx); goto corrupted; } } @@ -2287,18 +2310,14 @@ static int output_builtin_bin(struct depmod *depmod, FILE *out) { FILE *in; struct index_node *idx; - char infile[PATH_MAX], line[PATH_MAX], modname[PATH_MAX]; + char line[PATH_MAX], modname[PATH_MAX]; if (out == stdout) return 0; - snprintf(infile, sizeof(infile), "%s/modules.builtin", - depmod->cfg->dirname); - in = fopen(infile, "r"); - if (in == NULL) { - WRN("could not open %s: %m\n", infile); + in = dfdopen(depmod->cfg->dirname, "modules.builtin", O_RDONLY, "r"); + if (in == NULL) return 0; - } idx = index_create(); if (idx == NULL) {