btrfs-progs/common/fsfeatures.h
Qu Wenruo d8f1bd519f btrfs-progs: mkfs: fix a stack over-flow when features string are too long
[BUG]
Even with chunk_objectid bug fixed, mkfs.btrfs can still caused stack
overflow when enabling extent-tree-v2 feature (need experimental
features enabled):

  # ./mkfs.btrfs  -f -O extent-tree-v2 ~/test.img
  btrfs-progs v5.19.1
  See http://btrfs.wiki.kernel.org for more information.

  ERROR: superblock magic doesn't match
  NOTE: several default settings have changed in version 5.15, please make sure
        this does not affect your deployments:
        - DUP for metadata (-m dup)
        - enabled no-holes (-O no-holes)
        - enabled free-space-tree (-R free-space-tree)

  Label:              (null)
  UUID:               205c61e7-f58e-4e8f-9dc2-38724f5c554b
  Node size:          16384
  Sector size:        4096
  Filesystem size:    512.00MiB
  Block group profiles:
    Data:             single            8.00MiB
    Metadata:         DUP              32.00MiB
    System:           DUP               8.00MiB
  SSD detected:       no
  Zoned device:       no
  =================================================================
  [... Skip full ASAN output ...]
  ==65655==ABORTING

[CAUSE]
For experimental build, we have unified feature output, but the old
buffer size is only 64 bytes, which is too small to cover the new full
feature string:

  extref, skinny-metadata, no-holes, free-space-tree, block-group-tree, extent-tree-v2

Above feature string is already 84 bytes, over the 64 on-stack memory
size.

This can also be proved by the ASAN output:

  ==65655==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc4e03b1d0 at pc 0x7ff0fc05fafe bp 0x7ffc4e03ac60 sp 0x7ffc4e03a408
  WRITE of size 17 at 0x7ffc4e03b1d0 thread T0
      #0 0x7ff0fc05fafd in __interceptor_strcat /usr/src/debug/gcc/libsanitizer/asan/asan_interceptors.cpp:377
      #1 0x55cdb7b06ca5 in parse_features_to_string common/fsfeatures.c:316
      #2 0x55cdb7b06ce1 in btrfs_parse_fs_features_to_string common/fsfeatures.c:324
      #3 0x55cdb7a37226 in main mkfs/main.c:1783
      #4 0x7ff0fbe3c28f  (/usr/lib/libc.so.6+0x2328f)
      #5 0x7ff0fbe3c349 in __libc_start_main (/usr/lib/libc.so.6+0x23349)
      #6 0x55cdb7a2cb34 in _start ../sysdeps/x86_64/start.S:115

[FIX]
Introduce a new macro, BTRFS_FEATURE_STRING_BUF_SIZE, along with a new
sanity check helper, btrfs_assert_feature_buf_size().

The problem is I can not find a build time method to verify
BTRFS_FEATURE_STRING_BUF_SIZE is large enough to contain all feature
names, thus have to go the runtime function to do the BUG_ON() to verify
the macro size.

Now the minimal buffer size for experimental build is 138 bytes, just
bump it to 160 for future expansion.

And if further features go beyond that number, mkfs.btrfs/btrfs-convert
will immediately crash at that BUG_ON(), so we can definitely detect it.

Reviewed-by: Anand Jain <anand.jain@oracle.com>
Tested-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-10-11 09:08:12 +02:00

98 lines
3.7 KiB
C

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/
#ifndef __BTRFS_FSFEATURES_H__
#define __BTRFS_FSFEATURES_H__
#include "kerncompat.h"
#include <stdio.h>
#include "kernel-lib/sizes.h"
#define BTRFS_MKFS_DEFAULT_NODE_SIZE SZ_16K
/*
* Since one feature can set at least one bit in either
* incompat/compat_or/runtime flags, all mkfs features users should
* use this structure to parse the features.
*/
struct btrfs_mkfs_features {
u64 incompat_flags;
u64 compat_ro_flags;
u64 runtime_flags;
};
#define BTRFS_FEATURE_RUNTIME_QUOTA (1ULL << 0)
#define BTRFS_FEATURE_RUNTIME_LIST_ALL (1ULL << 1)
/*
* Such buffer size should be able to contain all feature string, with extra
* ", " for each feature.
*/
#define BTRFS_FEATURE_STRING_BUF_SIZE (160)
static const struct btrfs_mkfs_features btrfs_mkfs_default_features = {
.compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |
BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID,
.incompat_flags = BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |
BTRFS_FEATURE_INCOMPAT_NO_HOLES |
BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA,
};
/*
* Avoid multi-device features (RAID56 and RAID1C34), mixed bgs, and zoned
* mode for btrfs-convert, as all supported fses are single device fses.
*
* Features like compression is disabled in btrfs-convert by default, as
* data is reusing the old data from the source fs.
* Corresponding flag will be set when the first compression write happens.
*/
static const struct btrfs_mkfs_features btrfs_convert_allowed_features = {
.compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |
BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID |
BTRFS_FEATURE_COMPAT_RO_BLOCK_GROUP_TREE,
.incompat_flags = BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF |
BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL |
BTRFS_FEATURE_INCOMPAT_BIG_METADATA |
BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF |
BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA |
BTRFS_FEATURE_INCOMPAT_NO_HOLES,
.runtime_flags = BTRFS_FEATURE_RUNTIME_QUOTA,
};
void btrfs_list_all_fs_features(const struct btrfs_mkfs_features *allowed);
void btrfs_list_all_runtime_features(const struct btrfs_mkfs_features *allowed);
char *btrfs_parse_fs_features(char *namelist,
struct btrfs_mkfs_features *features);
char *btrfs_parse_runtime_features(char *namelist,
struct btrfs_mkfs_features *features);
void btrfs_process_fs_features(struct btrfs_mkfs_features *features);
void btrfs_process_runtime_features(struct btrfs_mkfs_features *features);
void btrfs_parse_fs_features_to_string(char *buf,
const struct btrfs_mkfs_features *features);
void btrfs_parse_runtime_features_to_string(char *buf,
const struct btrfs_mkfs_features *features);
void print_kernel_version(FILE *stream, u32 version);
u32 get_running_kernel_version(void);
int btrfs_check_nodesize(u32 nodesize, u32 sectorsize,
struct btrfs_mkfs_features *features);
int btrfs_check_sectorsize(u32 sectorsize);
int btrfs_check_features(const struct btrfs_mkfs_features *features,
const struct btrfs_mkfs_features *allowed);
int btrfs_tree_search2_ioctl_supported(int fd);
void btrfs_assert_feature_buf_size(void);
#endif