btrfs: Fix out of bounds access in btrfs_search_slot
[sfrench/cifs-2.6.git] / fs / btrfs / ctree.c
index 1e74cf82653271e7d5c7b6cbf930aea09751bc49..5361f69433a3a9d83a75cbbe8094f902df346671 100644 (file)
@@ -2774,6 +2774,8 @@ again:
                 * contention with the cow code
                 */
                if (cow) {
+                       bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
+
                        /*
                         * if we don't really need to cow this block
                         * then we don't want to set the path blocking,
@@ -2798,9 +2800,13 @@ again:
                        }
 
                        btrfs_set_path_blocking(p);
-                       err = btrfs_cow_block(trans, root, b,
-                                             p->nodes[level + 1],
-                                             p->slots[level + 1], &b);
+                       if (last_level)
+                               err = btrfs_cow_block(trans, root, b, NULL, 0,
+                                                     &b);
+                       else
+                               err = btrfs_cow_block(trans, root, b,
+                                                     p->nodes[level + 1],
+                                                     p->slots[level + 1], &b);
                        if (err) {
                                ret = err;
                                goto done;