Received: by 10.223.164.202 with SMTP id h10csp4389026wrb; Wed, 29 Nov 2017 05:44:03 -0800 (PST) X-Google-Smtp-Source: AGs4zMb410v0+EaH+Hpl/mPiqfE6p5dEXPH9ZHRTnpy6ZjZWOdKfOHrGmJS3AlO5qxjXguRHf47c X-Received: by 10.159.198.4 with SMTP id f4mr2890430plo.331.1511963043528; Wed, 29 Nov 2017 05:44:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511963043; cv=none; d=google.com; s=arc-20160816; b=OTPP/wLRylSM5LGmEvtW0ziJG/nlHRzE03uutnvTm4cgl6Qm0jsha4STsRhSlYoijd 7k15mfIMtAboodCTS45b9lYVOKCKfn4psNWLsQCCSf7lPa9plk2uNxmygQrpCSEkx1ft 3VgoQwDegbLBxXZKcqt742Vv+4Muw+R0cL1Bu218EelFWqcxsHUt7/iBjPLgGnA2xHoR CXMvNXfskLLNyYclgTPJHrOvZAd2jMXI0bG1aCutT0fZ90TAfmEYZPOlAbqr0MH9TATB KCO265YkqN8ZzddIXwdmXRhUAdB6AnASjhkp1AVSAJo4gBScmdqc/XWz/oEgaagRpJZM Bnbg== 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:arc-authentication-results; bh=6gRmY20C8oAdfqXNRjw28HKjJ33ZyjpDYwW+oia8m9I=; b=sOE10zZb0PowOKZFzbydeyipV1YZTRxWsXEqV0Btom5NEjS/U37tXOpkv1ratHlPV6 xkoZWbWBcEzlRjKhLjC4gt5aX++E05+ZT6CDTVqmFIW2ZgWZPgonlzgBA3BTXrqAGCLT RbTJ8y6kWzh9ng+dCGe+6IqmdYJ7a5WO+RlOrxylrEVoVNfkEDEuiUkuBu35l3JONCyq AhnEguQoSfqp7NxSHtAxsWa+j2b1qTCxY2XVz3g8ulLrU8wUAnQpD7mrr5TjMtFPkZRZ vCdr91JVKpxsS8KgT4k6JmSJivPPbUMxVITzCrLqdM99U+W9WhHLPR4R8JXXf7wQUGlr 8F7Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 31si1317198plk.345.2017.11.29.05.43.53; Wed, 29 Nov 2017 05:44:03 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754545AbdK2LQH (ORCPT + 70 others); Wed, 29 Nov 2017 06:16:07 -0500 Received: from mga06.intel.com ([134.134.136.31]:16136 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751936AbdK2LQF (ORCPT ); Wed, 29 Nov 2017 06:16:05 -0500 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Nov 2017 03:16:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,472,1505804400"; d="scan'208";a="13293492" Received: from elena-thinkpad-x230.fi.intel.com ([10.237.72.87]) by orsmga002.jf.intel.com with ESMTP; 29 Nov 2017 03:16:01 -0800 From: Elena Reshetova To: linux-nfs@vger.kernel.org Cc: linux-kernel@vger.kernel.org, bfields@fieldses.org, jlayton@kernel.org, trond.myklebust@primarydata.com, anna.schumaker@netapp.com, peterz@infradead.org, keescook@chromium.org, Elena Reshetova Subject: [PATCH 4/4] lockd: convert nlm_rqst.a_count from atomic_t to refcount_t Date: Wed, 29 Nov 2017 13:15:46 +0200 Message-Id: <1511954146-11793-5-git-send-email-elena.reshetova@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1511954146-11793-1-git-send-email-elena.reshetova@intel.com> References: <1511954146-11793-1-git-send-email-elena.reshetova@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org atomic_t variables are currently used to implement reference counters with the following properties: - counter is initialized to 1 using atomic_set() - a resource is freed upon counter reaching zero - once counter reaches zero, its further increments aren't allowed - counter schema uses basic atomic operations (set, inc, inc_not_zero, dec_and_test, etc.) Such atomic variables should be converted to a newly provided refcount_t type and API that prevents accidental counter overflows and underflows. This is important since overflows and underflows can lead to use-after-free situation and be exploitable. The variable nlm_rqst.a_count is used as pure reference counter. Convert it to refcount_t and fix up the operations. **Important note for maintainers: Some functions from refcount_t API defined in lib/refcount.c have different memory ordering guarantees than their atomic counterparts. The full comparison can be seen in https://lkml.org/lkml/2017/11/15/57 and it is hopefully soon in state to be merged to the documentation tree. Normally the differences should not matter since refcount_t provides enough guarantees to satisfy the refcounting use cases, but in some rare cases it might matter. Please double check that you don't have some undocumented memory guarantees for this variable usage. For the nlm_rqst.a_count it might make a difference in following places: - nlmclnt_release_call() and nlmsvc_release_call(): decrement in refcount_dec_and_test() only provides RELEASE ordering and control dependency on success vs. fully ordered atomic counterpart Suggested-by: Kees Cook Reviewed-by: David Windsor Reviewed-by: Hans Liljestrand Signed-off-by: Elena Reshetova --- fs/lockd/clntproc.c | 8 ++++---- fs/lockd/svcproc.c | 2 +- include/linux/lockd/lockd.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/fs/lockd/clntproc.c b/fs/lockd/clntproc.c index 112173d..a2c0dfc 100644 --- a/fs/lockd/clntproc.c +++ b/fs/lockd/clntproc.c @@ -204,7 +204,7 @@ struct nlm_rqst *nlm_alloc_call(struct nlm_host *host) for(;;) { call = kzalloc(sizeof(*call), GFP_KERNEL); if (call != NULL) { - atomic_set(&call->a_count, 1); + refcount_set(&call->a_count, 1); locks_init_lock(&call->a_args.lock.fl); locks_init_lock(&call->a_res.lock.fl); call->a_host = nlm_get_host(host); @@ -222,7 +222,7 @@ void nlmclnt_release_call(struct nlm_rqst *call) { const struct nlmclnt_operations *nlmclnt_ops = call->a_host->h_nlmclnt_ops; - if (!atomic_dec_and_test(&call->a_count)) + if (!refcount_dec_and_test(&call->a_count)) return; if (nlmclnt_ops && nlmclnt_ops->nlmclnt_release_call) nlmclnt_ops->nlmclnt_release_call(call->a_callback_data); @@ -678,7 +678,7 @@ nlmclnt_unlock(struct nlm_rqst *req, struct file_lock *fl) goto out; } - atomic_inc(&req->a_count); + refcount_inc(&req->a_count); status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req, NLMPROC_UNLOCK, &nlmclnt_unlock_ops); if (status < 0) @@ -769,7 +769,7 @@ static int nlmclnt_cancel(struct nlm_host *host, int block, struct file_lock *fl nlmclnt_setlockargs(req, fl); req->a_args.block = block; - atomic_inc(&req->a_count); + refcount_inc(&req->a_count); status = nlmclnt_async_call(nfs_file_cred(fl->fl_file), req, NLMPROC_CANCEL, &nlmclnt_cancel_ops); if (status == 0 && req->a_res.status == nlm_lck_denied) diff --git a/fs/lockd/svcproc.c b/fs/lockd/svcproc.c index 0d670c5..ea77c66 100644 --- a/fs/lockd/svcproc.c +++ b/fs/lockd/svcproc.c @@ -295,7 +295,7 @@ static void nlmsvc_callback_exit(struct rpc_task *task, void *data) void nlmsvc_release_call(struct nlm_rqst *call) { - if (!atomic_dec_and_test(&call->a_count)) + if (!refcount_dec_and_test(&call->a_count)) return; nlmsvc_release_host(call->a_host); kfree(call); diff --git a/include/linux/lockd/lockd.h b/include/linux/lockd/lockd.h index 86d012a..4fd95db 100644 --- a/include/linux/lockd/lockd.h +++ b/include/linux/lockd/lockd.h @@ -137,7 +137,7 @@ struct nlm_wait; */ #define NLMCLNT_OHSIZE ((__NEW_UTS_LEN) + 10u) struct nlm_rqst { - atomic_t a_count; + refcount_t a_count; unsigned int a_flags; /* initial RPC task flags */ struct nlm_host * a_host; /* host handle */ struct nlm_args a_args; /* arguments */ -- 2.7.4 From 1585306943871379645@xxx Tue Nov 28 10:55:15 +0000 2017 X-GM-THRID: 1585305481955076361 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread