From patchwork Mon Feb 22 18:14:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Claudi X-Patchwork-Id: 12099269 X-Patchwork-Delegate: stephen@networkplumber.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93EBFC433E0 for ; Mon, 22 Feb 2021 18:22:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4F9E464EF5 for ; Mon, 22 Feb 2021 18:22:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232421AbhBVSW0 (ORCPT ); Mon, 22 Feb 2021 13:22:26 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:21950 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232362AbhBVSV1 (ORCPT ); Mon, 22 Feb 2021 13:21:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614018002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=18wy2qx4bycUMsn6BjZIIZfFYeIEKj+LojFrwoZ5MnE=; b=XSF1VyhvEe9c5QhbyfVvMRjZf4F07h7WizlBiYmij17sDHAVj0tn3ihRkfiMQolzMg1HwU 0eFNYbviRfH3kblPa39d7DT/r+Vk1TXRfCATYjEW53PvhGADuoflh12N6gaMul5VsE4T1u gF/LKW3vtMXd/oFSnZnOmv/Olhc9SzA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-146-ARPGZkogMu-VoFgihiu5CA-1; Mon, 22 Feb 2021 13:20:00 -0500 X-MC-Unique: ARPGZkogMu-VoFgihiu5CA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 434F66D4F2; Mon, 22 Feb 2021 18:19:58 +0000 (UTC) Received: from localhost.localdomain (ovpn-115-141.ams2.redhat.com [10.36.115.141]) by smtp.corp.redhat.com (Postfix) with ESMTP id 69CF610016FA; Mon, 22 Feb 2021 18:19:57 +0000 (UTC) From: Andrea Claudi To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com Subject: [PATCH iproute2 v2 1/2] lib/fs: avoid double call to mkdir on make_path() Date: Mon, 22 Feb 2021 19:14:31 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com make_path() function calls mkdir two times in a row. The first one it stores mkdir return code, and then it calls it again to check for errno. This seems unnecessary, as we can use the return code from the first call and check for errno if not 0. Fixes: ac3415f5c1b1d ("lib/fs: Fix and simplify make_path()") Acked-by: Phil Sutter Signed-off-by: Andrea Claudi --- lib/fs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fs.c b/lib/fs.c index 4b90a704..2ae506ec 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -253,7 +253,7 @@ int make_path(const char *path, mode_t mode) *delim = '\0'; rc = mkdir(dir, mode); - if (mkdir(dir, mode) != 0 && errno != EEXIST) { + if (rc && errno != EEXIST) { fprintf(stderr, "mkdir failed for %s: %s\n", dir, strerror(errno)); goto out; From patchwork Mon Feb 22 18:14:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrea Claudi X-Patchwork-Id: 12099273 X-Patchwork-Delegate: stephen@networkplumber.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58CB1C433DB for ; Mon, 22 Feb 2021 18:23:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2311A64EF5 for ; Mon, 22 Feb 2021 18:23:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232438AbhBVSWg (ORCPT ); Mon, 22 Feb 2021 13:22:36 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:28666 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232394AbhBVSV2 (ORCPT ); Mon, 22 Feb 2021 13:21:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614018002; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=77O8dSXMdWHkv4MTs443H2Uihx057ilNee9Auo5qZRc=; b=f2sJnfdG/XqzlAwF9/DswV/XPFlKZ8ozedEboJSF8KwnycOJbGNYYK9q9clb+vZlDHKK+D c6Mue+yNiDnENgZOFmKGI/vdeNCZ4Oik6yUVltu0pCgzXC5TA2FhUOK/j35rqVr23hh0I9 lVghCDeh6lWoqVlnmKyujrqXWZhQbts= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-389-dyXNqRXTOEWo6ep7T_T0pQ-1; Mon, 22 Feb 2021 13:20:00 -0500 X-MC-Unique: dyXNqRXTOEWo6ep7T_T0pQ-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 75AF4107ACE8; Mon, 22 Feb 2021 18:19:59 +0000 (UTC) Received: from localhost.localdomain (ovpn-115-141.ams2.redhat.com [10.36.115.141]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8F0731001281; Mon, 22 Feb 2021 18:19:58 +0000 (UTC) From: Andrea Claudi To: netdev@vger.kernel.org Cc: stephen@networkplumber.org, dsahern@gmail.com Subject: [PATCH iproute2 v2 2/2] lib/fs: Fix single return points for get_cgroup2_* Date: Mon, 22 Feb 2021 19:14:32 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com Functions get_cgroup2_id() and get_cgroup2_path() may call close() with a negative argument. Avoid that making the calls conditional on the file descriptors. get_cgroup2_path() may also return NULL leaking a file descriptor. Ensure this does not happen using a single return point. Fixes: d5e6ee0dac64 ("ss: introduce cgroup2 cache and helper functions") Fixes: 8f1cd119b377 ("lib: fix checking of returned file handle size for cgroup") Signed-off-by: Andrea Claudi --- lib/fs.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/fs.c b/lib/fs.c index 2ae506ec..ee0b130b 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -157,7 +157,8 @@ __u64 get_cgroup2_id(const char *path) memcpy(cg_id.bytes, fhp->f_handle, sizeof(__u64)); out: - close(mnt_fd); + if (mnt_fd >= 0) + close(mnt_fd); free(mnt); return cg_id.id; @@ -179,16 +180,16 @@ char *get_cgroup2_path(__u64 id, bool full) char *path = NULL; char fd_path[64]; int link_len; - char *mnt; + char *mnt = NULL; if (!id) { fprintf(stderr, "Invalid cgroup2 ID\n"); - return NULL; + goto out; } mnt = find_cgroup2_mount(false); if (!mnt) - return NULL; + goto out; mnt_fd = open(mnt, O_RDONLY); if (mnt_fd < 0) { @@ -225,8 +226,10 @@ char *get_cgroup2_path(__u64 id, bool full) "Failed to allocate memory for cgroup2 path\n"); out: - close(fd); - close(mnt_fd); + if (fd >= 0) + close(fd); + if (mnt_fd >= 0) + close(mnt_fd); free(mnt); return path;