From patchwork Fri Dec 5 15:40:07 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 5444491 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id BA802BEEA8 for ; Fri, 5 Dec 2014 15:40:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E96FD201C8 for ; Fri, 5 Dec 2014 15:40:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0AFF220145 for ; Fri, 5 Dec 2014 15:40:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751085AbaLEPkX (ORCPT ); Fri, 5 Dec 2014 10:40:23 -0500 Received: from mail-la0-f43.google.com ([209.85.215.43]:53868 "EHLO mail-la0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751154AbaLEPkV (ORCPT ); Fri, 5 Dec 2014 10:40:21 -0500 Received: by mail-la0-f43.google.com with SMTP id s18so367808lam.30 for ; Fri, 05 Dec 2014 07:40:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Z/39uNj61TNe4ujsZRTVFQNuNK5tStm25z/aTKXmPuc=; b=Y/fE3pcxQoa3rGTl+xsIzXb0bVtzdk6VvsCSeEmtwHOy40Ot7qTdhCxw7h+hEPuXOe ZliYf9hT/KLRTcCTi/BTTs5Vn3JXo/a4Gi2ciOTh22afzytUk6VRN9nRgO+2K2/aGKTP IQOEO+jdT0BxAuSNSwvFG+dMi8vJGxn0n7rGU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Z/39uNj61TNe4ujsZRTVFQNuNK5tStm25z/aTKXmPuc=; b=MHkH4TsBMhdMP9/INUnxerGE75jLou9QTjjvEK+KvPDdbhf636glDdy/npFHi6sar8 E/4qo5HK3pQRH0kDBQITBlNDod77q1ruTJ7jbqymHAjs+hk64ef3/leaYqean8N8LMLc m1LDM+2qL79og2zcxFLDn7m/msNTZ0DI7NlV3m2xfTLfpX/1aQSAf0sy0k/2V09bQ8+F PdDNU3G7Y1OABJDKyvzCi64Jw9x4qQr2R/bRnLVIrpkKvhoDRihijPnKD8wtJbJiofoq D/DXCUrtguLlhsWz8kV6TaNlrV83llKCM4aH7h+ZkLckv/XJS4s9ZaIoSnvJwW9wOAkv Go6A== X-Gm-Message-State: ALoCoQmVSEMB0v8XaWhu6mXDbB68xlHwNexyX/z+OU/yD1qWuYpMT+akSFZmi+/nMyKl+K5AXUru X-Received: by 10.112.155.72 with SMTP id vu8mr3589086lbb.30.1417794020044; Fri, 05 Dec 2014 07:40:20 -0800 (PST) Received: from spencer.imf.au.dk ([130.225.20.51]) by mx.google.com with ESMTPSA id b4sm8428131lbp.17.2014.12.05.07.40.18 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 Dec 2014 07:40:19 -0800 (PST) From: Rasmus Villemoes To: "J. Bruce Fields" Cc: Jeff Layton , Rasmus Villemoes , , linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] fs: nfsd: Fix signedness bug in compare_blob Date: Fri, 5 Dec 2014 16:40:07 +0100 Message-Id: <1417794008-16944-1-git-send-email-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.0.4 In-Reply-To: <1417706452-30204-1-git-send-email-linux@rasmusvillemoes.dk> References: <1417706452-30204-1-git-send-email-linux@rasmusvillemoes.dk> Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD,UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Bugs similar to the one in acbbe6fbb240 (kcmp: fix standard comparison bug) are in rich supply. In this variant, the problem is that struct xdr_netobj::len has type unsigned int, so the expression o1->len - o2->len _also_ has type unsigned int; it has completely well-defined semantics, and the result is some non-negative integer, which is always representable in a long long. But this means that if the conditional triggers, we are guaranteed to return a positive value from compare_blob. In this case it could be fixed by - res = o1->len - o2->len; + res = (long long)o1->len - (long long)o2->len; but I'd rather eliminate the usually broken 'return a - b;' idiom. Reviewed-by: Jeff Layton Cc: Signed-off-by: Rasmus Villemoes --- Notes: How this could ever have worked is beyond me - compare_blob seems to be used to maintain an rbtree, and I wouldn't expect rbtrees to behave well if the comparison function doesn't satisfy the basic invariant sign(cmp(a, b)) == -sign(cmp(b, a)). v2: Same patch. Slightly less generic subject. Added Jeff's Reviewed-by and Cc stable. fs/nfsd/nfs4state.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/fs/nfsd/nfs4state.c b/fs/nfsd/nfs4state.c index e9c3afe4b5d3..d504cd6927f8 100644 --- a/fs/nfsd/nfs4state.c +++ b/fs/nfsd/nfs4state.c @@ -1711,15 +1711,14 @@ static int copy_cred(struct svc_cred *target, struct svc_cred *source) return 0; } -static long long +static int compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) { - long long res; - - res = o1->len - o2->len; - if (res) - return res; - return (long long)memcmp(o1->data, o2->data, o1->len); + if (o1->len < o2->len) + return -1; + if (o1->len > o2->len) + return 1; + return memcmp(o1->data, o2->data, o1->len); } static int same_name(const char *n1, const char *n2) @@ -1907,7 +1906,7 @@ add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) static struct nfs4_client * find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) { - long long cmp; + int cmp; struct rb_node *node = root->rb_node; struct nfs4_client *clp;