Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp628191ybi; Fri, 26 Jul 2019 16:19:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqxm8Cr13k2xsCaECBvLWZe6dRwQJcJB68b2K7WMr2ftwXQvRCcUnnElLgr8VHNwLZOSoRJz X-Received: by 2002:a17:90a:2486:: with SMTP id i6mr99451807pje.125.1564183150632; Fri, 26 Jul 2019 16:19:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564183150; cv=none; d=google.com; s=arc-20160816; b=RwoVjHQS/73pNfEW6qAcFjXoskX5YyJIf49vQBAIIDstDLODoVfJyGsoBgjQA10Pr2 YmdiI4ZP4MnTB6KL01uDLeyNp0HvriRiS3XCjpWV1YJwqgzflWejoIUuF7rSJ2524kyj rsI4/HMggztLb+Zg6aWlNQTx7OSEDtnDGx1HHLeaH8ac/02XG0AJ4Jdk1kkXEXPvw6aU NlhjE+uBKId0VLCf3IAnUUbDpyz932NDUlKJvw8lTjxEY9FODbfCeVviFuXVinfBLlIO 8DAXrAViPLtXsSjOnUn70lcfWHPc8EqlZlZt0A2Lw7qfOXH/8a+NNJ+JtFIN6fXryv/q D6Ew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=GYZZyUvE1yNzQgHcXPt2VZWRj+LXtXD5q0WNTVZEr5U=; b=05Q+WqhCpAobrzZ4rw3AYTJDyVLEa2RmmUVrCyxkwNe2ItuEnVWW4Dd2qAaGEa5BiV OnaULLKGng7jlZFFAe8hWY0ulJvuK91VFHEWpPio6bb6TCSib7TaSfSkclmL2WJ/3Z5T iIfp01o1ipz/YdRGRQOfB1qQDiGS0MwNCkB4LTuFrumzO/bPZ64yW11cGR5e1RZMM7Br AT9bDqGvcGoMJLH+KpKckdCvxlndiv1NuJbgKGtOeK+qsO+hiM9GeFsCKKMU3S7xdDr7 oAbuEOchVxCtyh3HADtRhXpzcWHqhSCCWSFLBVXTCk32RzhuKApykFm7T9+MZrgHx/q1 7csw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n2si1961881pga.426.2019.07.26.16.18.45; Fri, 26 Jul 2019 16:19:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-nfs-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727267AbfGZWdC (ORCPT + 99 others); Fri, 26 Jul 2019 18:33:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32416 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726364AbfGZWdC (ORCPT ); Fri, 26 Jul 2019 18:33:02 -0400 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 42F0D300CB07; Fri, 26 Jul 2019 22:33:02 +0000 (UTC) Received: from dwysocha.rdu.csb (dhcp-12-212-173.gsslab.rdu.redhat.com [10.12.212.173]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C01EE5DE6F; Fri, 26 Jul 2019 22:33:01 +0000 (UTC) From: Dave Wysochanski To: bfields@fieldses.org Cc: neilb@suse.com, linux-nfs@vger.kernel.org Subject: [RFC PATCH] SUNRPC: Track writers of the 'channel' file to improve cache_listeners_exist Date: Fri, 26 Jul 2019 18:33:01 -0400 Message-Id: <1564180381-9916-1-git-send-email-dwysocha@redhat.com> In-Reply-To: <20190725185421.GA15073@fieldses.org> References: <20190725185421.GA15073@fieldses.org> 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.47]); Fri, 26 Jul 2019 22:33:02 +0000 (UTC) Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org The sunrpc cache interface is susceptible to being fooled by a rogue process just reading a 'channel' file. If this happens the kernel may think a valid daemon exists to service the cache when it does not. For example, the following may fool the kernel: cat /proc/net/rpc/auth.unix.gid/channel Change the tracking of readers to writers when considering whether a listener exists as all valid daemon processes either open a channel file O_RDWR or O_WRONLY. While this does not prevent a rogue process from "stealing" a message from the kernel, it does at least improve the kernels perception of whether a valid process servicing the cache exists. Signed-off-by: Dave Wysochanski --- include/linux/sunrpc/cache.h | 6 +++--- net/sunrpc/cache.c | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/linux/sunrpc/cache.h b/include/linux/sunrpc/cache.h index c7f38e8..f7d086b 100644 --- a/include/linux/sunrpc/cache.h +++ b/include/linux/sunrpc/cache.h @@ -107,9 +107,9 @@ struct cache_detail { /* fields for communication over channel */ struct list_head queue; - atomic_t readers; /* how many time is /chennel open */ - time_t last_close; /* if no readers, when did last close */ - time_t last_warn; /* when we last warned about no readers */ + atomic_t writers; /* how many time is /channel open */ + time_t last_close; /* if no writers, when did last close */ + time_t last_warn; /* when we last warned about no writers */ union { struct proc_dir_entry *procfs; diff --git a/net/sunrpc/cache.c b/net/sunrpc/cache.c index 6f1528f..a6a6190 100644 --- a/net/sunrpc/cache.c +++ b/net/sunrpc/cache.c @@ -373,7 +373,7 @@ void sunrpc_init_cache_detail(struct cache_detail *cd) spin_lock(&cache_list_lock); cd->nextcheck = 0; cd->entries = 0; - atomic_set(&cd->readers, 0); + atomic_set(&cd->writers, 0); cd->last_close = 0; cd->last_warn = -1; list_add(&cd->others, &cache_list); @@ -1029,11 +1029,13 @@ static int cache_open(struct inode *inode, struct file *filp, } rp->offset = 0; rp->q.reader = 1; - atomic_inc(&cd->readers); + spin_lock(&queue_lock); list_add(&rp->q.list, &cd->queue); spin_unlock(&queue_lock); } + if (filp->f_mode & FMODE_WRITE) + atomic_inc(&cd->writers); filp->private_data = rp; return 0; } @@ -1062,8 +1064,10 @@ static int cache_release(struct inode *inode, struct file *filp, filp->private_data = NULL; kfree(rp); + } + if (filp->f_mode & FMODE_WRITE) { + atomic_dec(&cd->writers); cd->last_close = seconds_since_boot(); - atomic_dec(&cd->readers); } module_put(cd->owner); return 0; @@ -1171,7 +1175,7 @@ static void warn_no_listener(struct cache_detail *detail) static bool cache_listeners_exist(struct cache_detail *detail) { - if (atomic_read(&detail->readers)) + if (atomic_read(&detail->writers)) return true; if (detail->last_close == 0) /* This cache was never opened */ -- 1.8.3.1