Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp2663964ybh; Fri, 24 Jul 2020 20:25:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcxQF2GcdR1BotvJA8cCyfJlEPFrf6RhVWvwEDDDhHxLtKc9H61CeqYAyDWQfSpPmlE32j X-Received: by 2002:aa7:dad6:: with SMTP id x22mr11428832eds.310.1595647549467; Fri, 24 Jul 2020 20:25:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595647549; cv=none; d=google.com; s=arc-20160816; b=vEcV6vhnmX/2rFX88ruS0wiByB2GW2IJnLtqBYbjOWZn4xNu9IVi4PV1nS+zEQ5Oa/ OxPOvpprjMKodg/uPnAUbP9NVCVMlLoQEZb7+3kGAAmES4uOztX2cUPZ7jT9E7c8IuOI yjlpwZMNg3Q99m/ZGKl9uATYPLk4Sm8n0f+BSm87FrDMn6sdP5nXSbQtRVmcnE4/z9r2 RwnLkA6lD6R0Wpdqr2oR/f3eBVemYhHFqNA7n40LkRVlLQM0aHXb5utD5zzgdCMSSlRu gexG5sroY6XPY+k4chZvEC8EQTE83X6beYARcIhX3qgtgZltlI+oGDnIoSXvXUY4mVef 6bag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:organization:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:ironport-sdr:ironport-sdr; bh=nHYRJekLXl1jVe4Jve5ZPN3newCrN944HQhJvO3ILfw=; b=ePUmJE0i/n+H1AewG9e0yyiOZIuRwgagR8bmWmeXe0hw3fjJNW4OpEMAEsaiSrfTty 9ADVc2mJX9SZS9Y3J4Bwsq6fDUV2GVwC3Ha5lZZ/xT7sFyzZ5leHTGCaiA16J148FzZ8 oAMW6A6xP3dTI+QFZ5qD+jzgHBrjp7wywVQwiiwTt3Etfhywjn48UxT4r/tV3/lLnOp2 jTOW4XJWDvErlSByhtev8aEZTEAtkGMakl7CTJz4diIzB00qw7bAitJdXBQEFPTzrpVu CEH60XUaiVOHqzAdRydMeoyS5oh5hnLDcO6R/gcl0eGoHDiBMIHYfUOiaevliqddlD2U 7NmQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t28si1581660edc.97.2020.07.24.20.25.25; Fri, 24 Jul 2020 20:25:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726686AbgGYDZW (ORCPT + 99 others); Fri, 24 Jul 2020 23:25:22 -0400 Received: from mga04.intel.com ([192.55.52.120]:10202 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726572AbgGYDZW (ORCPT ); Fri, 24 Jul 2020 23:25:22 -0400 IronPort-SDR: o9RvZeBGp7YEphIY6umZ+RWxEQzbk+q1PBhyJFp4EgJ+gNZ1reYjjrGVopff1x0qF8xKs9CIpg XRXMGqAUr7ow== X-IronPort-AV: E=McAfee;i="6000,8403,9692"; a="148297666" X-IronPort-AV: E=Sophos;i="5.75,392,1589266800"; d="scan'208";a="148297666" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Jul 2020 20:25:21 -0700 IronPort-SDR: eOIC+S4SulEMj5oJcjW9Qdy4I/m4tVpBq2wf0qTX16TbvS5WituY8v9vhrcYi+Sw5+PB1sWWu/ Ta50tujzkKYw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,392,1589266800"; d="scan'208";a="289178631" Received: from jcrametz-mobl.ger.corp.intel.com (HELO localhost) ([10.252.58.73]) by orsmga006.jf.intel.com with ESMTP; 24 Jul 2020 20:25:18 -0700 Date: Sat, 25 Jul 2020 06:25:16 +0300 From: Jarkko Sakkinen To: David Howells Cc: torvalds@linux-foundation.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] watch_queue: Limit the number of watches a user can hold Message-ID: <20200725032516.GA78242@linux.intel.com> References: <159562904644.2287160.13294507067766261970.stgit@warthog.procyon.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <159562904644.2287160.13294507067766261970.stgit@warthog.procyon.org.uk> Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jul 24, 2020 at 11:17:26PM +0100, David Howells wrote: > Impose a limit on the number of watches that a user can hold so that they > can't use this mechanism to fill up all the available memory. > > This is done by putting a counter in user_struct that's incremented when a > watch is allocated and decreased when it is released. If the number > exceeds the RLIMIT_NOFILE limit, the watch is rejected with EAGAIN. > > This can be tested by the following means: > > (1) Create a watch queue and attach it to fd 5 in the program given - in > this case, bash: > > keyctl watch_session /tmp/nlog /tmp/gclog 5 bash > > (2) In the shell, set the maximum number of files to, say, 99: > > ulimit -n 99 > > (3) Add 200 keyrings: > > for ((i=0; i<200; i++)); do keyctl newring a$i @s || break; done > > (4) Try to watch all of the keyrings: > > for ((i=0; i<200; i++)); do echo $i; keyctl watch_add 5 %:a$i || break; done > > This should fail when the number of watches belonging to the user hits > 99. > > (5) Remove all the keyrings and all of those watches should go away: > > for ((i=0; i<200; i++)); do keyctl unlink %:a$i; done > > (6) Kill off the watch queue by exiting the shell spawned by > watch_session. > > Fixes: c73be61cede5 ("pipe: Add general notification queue support") > Reported-by: Linus Torvalds > Signed-off-by: David Howells > --- > > include/linux/sched/user.h | 3 +++ > kernel/watch_queue.c | 8 ++++++++ > 2 files changed, 11 insertions(+) > > diff --git a/include/linux/sched/user.h b/include/linux/sched/user.h > index 917d88edb7b9..a8ec3b6093fc 100644 > --- a/include/linux/sched/user.h > +++ b/include/linux/sched/user.h > @@ -36,6 +36,9 @@ struct user_struct { > defined(CONFIG_NET) || defined(CONFIG_IO_URING) > atomic_long_t locked_vm; > #endif > +#ifdef CONFIG_WATCH_QUEUE > + atomic_t nr_watches; /* The number of watches this user currently has */ > +#endif > > /* Miscellaneous per-user rate limit */ > struct ratelimit_state ratelimit; > diff --git a/kernel/watch_queue.c b/kernel/watch_queue.c > index f74020f6bd9d..0ef8f65bd2d7 100644 > --- a/kernel/watch_queue.c > +++ b/kernel/watch_queue.c > @@ -393,6 +393,7 @@ static void free_watch(struct rcu_head *rcu) > struct watch *watch = container_of(rcu, struct watch, rcu); > > put_watch_queue(rcu_access_pointer(watch->queue)); > + atomic_dec(&watch->cred->user->nr_watches); > put_cred(watch->cred); > } > > @@ -452,6 +453,13 @@ int add_watch_to_object(struct watch *watch, struct watch_list *wlist) > watch->cred = get_current_cred(); > rcu_assign_pointer(watch->watch_list, wlist); > > + if (atomic_inc_return(&watch->cred->user->nr_watches) > > + task_rlimit(current, RLIMIT_NOFILE)) { > + atomic_dec(&watch->cred->user->nr_watches); > + put_cred(watch->cred); > + return -EAGAIN; > + } > + > spin_lock_bh(&wqueue->lock); > kref_get(&wqueue->usage); > kref_get(&watch->usage); > > Reviewed-by: Jarkko Sakkinen David, BTW, would it be possible to push keyrings to lore.kernel.org? I don't have an archive for keyrings, which means that I cannot push this forward. /Jarkko