From patchwork Tue Apr 16 00:47:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre Bourdon X-Patchwork-Id: 1085991 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="YUt9+8Om"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 44jmtK3hj9z9s55 for ; Tue, 16 Apr 2019 10:47:35 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 51B7EC21E35; Tue, 16 Apr 2019 00:47:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 88560C21CB1; Tue, 16 Apr 2019 00:47:26 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 17A75C21CB1; Tue, 16 Apr 2019 00:47:25 +0000 (UTC) Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) by lists.denx.de (Postfix) with ESMTPS id 673A4C21C29 for ; Tue, 16 Apr 2019 00:47:24 +0000 (UTC) Received: by mail-ed1-f48.google.com with SMTP id j20so6148104edq.10 for ; Mon, 15 Apr 2019 17:47:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=pTf5R/xBMDxPYZNYZL7fBccmfItQXIsMSwOQq0WBKFQ=; b=YUt9+8OmQ9+xXO0a7m7+beazTma4h5/DkXKKhuXZTXqnXEul69lTwbzJTz12oXzqnx +gs8XGm7BYRbS8GTbd1EH7twX/fmPZdbd8mbLg0KugEl6W3bUoZHN5glkFZU5kEqTog1 E+ViDORMe/YNQIXnY5+pGrGIprapYSShONxuZT6idMnvHhG4DFmLlF73qz0TTYnxagpc Vx7LQfse/bCvR/ITIKIotAsztANrkgnlhuKGVs4mKQgxFe9YUqM3tqnUl1D2sNeJs1+Y 4JLakQAAiS58D/CNNVgOs7pQo3vwzVaXmZnOTvwzo27cgkzdNSlvS7zxxk+pFNbsmRbA QMuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=pTf5R/xBMDxPYZNYZL7fBccmfItQXIsMSwOQq0WBKFQ=; b=t9L1MYnNjv/SGop5tRwG7xeT0hmPKKSTHCbX0nuc5X1y1z57RxavTs+P+I38MVNGH/ w+BiQCDW48J5Rag/X8qbgysBbzqG1QiZTg5ptEy/9TWbm1qOXmKmL7FfYaEcImmkPd/W hDqnbEOtlaTpLq1UozhWPiRyprJD3xI4h3UVi6tN/BM2Um3ZHsLnmI8eeMM6dbFJ+HeP 0+xrSfSG/TsmRZbsL2iEFHA4hVq+hbC/BQiFmrpVaCPQ+xxS47lz624I+2VYQhU2mdtm n0hCXyiZwim78N7Zf+QF1n3HQs0JydWm7Y6EHnY1/7Y6/XD9mv4ZxvlRhJWK7fEqZF0b LROA== X-Gm-Message-State: APjAAAVptl/tOfV70GBynnbNd7LK61SvcoFOHzwkHjyGqNabK1DISIvP DlJsaRsc8nE8bTKmyKfQDESTzFCJR9Q= X-Google-Smtp-Source: APXvYqxZpYWdhSacOguNeRi3DL80fNsE8KEGQttNOh7hgOMfuuf3RDIEHfYK8Xhjo3Om6ZJ1l8+WQQ== X-Received: by 2002:a50:8786:: with SMTP id a6mr18216570eda.8.1555375643677; Mon, 15 Apr 2019 17:47:23 -0700 (PDT) Received: from lowell.delroth.net ([2a02:168:6426::bb2]) by smtp.gmail.com with ESMTPSA id j15sm8811915edt.47.2019.04.15.17.47.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Apr 2019 17:47:22 -0700 (PDT) From: Pierre Bourdon To: u-boot@lists.denx.de Date: Tue, 16 Apr 2019 02:47:14 +0200 Message-Id: <20190416004714.27299-1-delroth@gmail.com> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 Subject: [U-Boot] [PATCH v2] fs: btrfs: fix btrfs_search_tree invalid results X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" btrfs_search_tree should return the first item in the tree that is greater or equal to the searched item. The search algorithm did not properly handle the edge case where the searched item is higher than the last item of the node but lower than the first item of the next node. Instead of properly returning the first item of the next node, it was returning an invalid path pointer (pointing to a non-existent item after the last item of the node + 1). This fixes two issues in the btrfs driver: - Looking for a ROOT_ITEM could fail if it was the first item of its leaf node. - Iterating through DIR_INDEX entries (for readdir) could fail if the first DIR_INDEX entry was the first item of a leaf node. Signed-off-by: Pierre Bourdon Cc: Marek Behun --- v2: coding style (braces), clearer comment fs/btrfs/ctree.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c index d248d79932..7fae383f15 100644 --- a/fs/btrfs/ctree.c +++ b/fs/btrfs/ctree.c @@ -185,10 +185,20 @@ int btrfs_search_tree(const struct btrfs_root *root, struct btrfs_key *key, p->slots[lvl] = slot; p->nodes[lvl] = buf; - if (lvl) + if (lvl) { logical = buf->node.ptrs[slot].blockptr; - else + } else { + /* + * The path might be invalid if: + * cur leaf max < searched value < next leaf min + * + * Jump to the next valid element if it exists. + */ + if (slot >= buf->header.nritems) + if (btrfs_next_slot(p) < 0) + goto err; break; + } } return 0;