github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/tools/create-buildroot-image.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2021 syzkaller project authors. All rights reserved.
     3  # Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     4  
     5  # This script builds a buildroot-based Linux image.
     6  # It should be run from a buildroot checkout (git://git.buildroot.net/buildroot) as:
     7  # TARGETARCH={amd64,arm64,arm,riscv64,s390x,mips64le,ppc64le} [NOMAKE=yes] create-buildroot-image.sh
     8  # If no NOMAKE=yes is specified, then it will just prepare the buildroot config,
     9  # but will not run the final make.
    10  # For amd64 and arm64 it creates a bootable image with root partition
    11  # on /dev/sda1 in output/images/disk.img file.
    12  # For other architectures it creates a non-bootable disk
    13  # suitable qemu injected boot with root partition on /dev/sda
    14  # in output/images/rootfs.ext4 file.
    15  # Note: the image requires at least kernel v4.19
    16  # (otherwise glibc complains about unsupported kernel version).
    17  
    18  set -eux
    19  
    20  NOMAKE="${NOMAKE:-}"
    21  TARGETARCH="${TARGETARCH:-amd64}"
    22  case "$TARGETARCH" in
    23  	amd64)
    24  		DEFCONFIG="pc_x86_64_bios_defconfig";;
    25  	arm64)
    26  		DEFCONFIG="aarch64_efi_defconfig";;
    27  	arm)
    28  		DEFCONFIG="qemu_arm_vexpress_defconfig";;
    29  	riscv64)
    30  		DEFCONFIG="qemu_riscv64_virt_defconfig";;
    31  	s390x)
    32  		DEFCONFIG="qemu_s390x_defconfig";;
    33  	mips64le)
    34  		DEFCONFIG="qemu_mips64r6el_malta_defconfig";;
    35  	ppc64le)
    36  		DEFCONFIG="qemu_ppc64le_pseries_defconfig";;
    37  	*)
    38  		echo "unsupported TARGETARCH=${TARGETARCH}"
    39  		exit 1;;
    40  esac
    41  
    42  git fetch origin
    43  git checkout 2022.08.2
    44  
    45  make "${DEFCONFIG}"
    46  
    47  # Common configs for all architectures.
    48  cat >>.config <<EOF
    49  BR2_TARGET_GENERIC_HOSTNAME="syzkaller"
    50  BR2_TARGET_GENERIC_ISSUE="syzkaller"
    51  BR2_ROOTFS_POST_FAKEROOT_SCRIPT="./rootfs_script.sh"
    52  BR2_TOOLCHAIN_BUILDROOT_GLIBC=y
    53  BR2_PACKAGE_DHCPCD=y
    54  BR2_PACKAGE_OPENSSH=y
    55  
    56  # This slows down boot.
    57  # BR2_PACKAGE_URANDOM_SCRIPTS is not set
    58  
    59  BR2_TARGET_ROOTFS_EXT2_SIZE="1G"
    60  # Slightly more interesting and realistic options.
    61  BR2_TARGET_ROOTFS_EXT2_MKFS_OPTIONS="-O 64bit,ext_attr,encrypt,extents,huge_file,flex_bg,dir_nlink,sparse_super,resize_inode,has_journal"
    62  
    63  # Install firmware for USB devices we can connect during fuzzing.
    64  BR2_PACKAGE_LINUX_FIRMWARE=y
    65  BR2_PACKAGE_LINUX_FIRMWARE_MEDIATEK_MT7650=y
    66  BR2_PACKAGE_LINUX_FIRMWARE_MEDIATEK_MT7601U=y
    67  BR2_PACKAGE_LINUX_FIRMWARE_MEDIATEK_MT7610E=y
    68  BR2_PACKAGE_LINUX_FIRMWARE_MEDIATEK_MT76X2E=y
    69  BR2_PACKAGE_LINUX_FIRMWARE_AR3012_USB=y
    70  BR2_PACKAGE_LINUX_FIRMWARE_BRCM_BCM43XX=y
    71  BR2_PACKAGE_LINUX_FIRMWARE_BRCM_BCM43XXX=y
    72  BR2_PACKAGE_LINUX_FIRMWARE_LIBERTAS_USB8388_V9=y
    73  BR2_PACKAGE_LINUX_FIRMWARE_LIBERTAS_USB8388_OLPC=y
    74  BR2_PACKAGE_LINUX_FIRMWARE_LIBERTAS_USB_THINFIRM=y
    75  BR2_PACKAGE_LINUX_FIRMWARE_MWIFIEX_USB8797=y
    76  BR2_PACKAGE_LINUX_FIRMWARE_MWIFIEX_USB8801=y
    77  BR2_PACKAGE_LINUX_FIRMWARE_MWIFIEX_USB8897=y
    78  BR2_PACKAGE_LINUX_FIRMWARE_RALINK_RT61=y
    79  BR2_PACKAGE_LINUX_FIRMWARE_RALINK_RT73=y
    80  BR2_PACKAGE_LINUX_FIRMWARE_RALINK_RT2XX=y
    81  BR2_PACKAGE_LINUX_FIRMWARE_AS102=y
    82  BR2_PACKAGE_LINUX_FIRMWARE_DIB0700=y
    83  BR2_PACKAGE_LINUX_FIRMWARE_ITETECH_IT9135=y
    84  BR2_PACKAGE_LINUX_FIRMWARE_CX231XX=y
    85  BR2_PACKAGE_LINUX_FIRMWARE_QUALCOMM_WIL6210=y
    86  # This one we could use, but it breaks buildroot:
    87  # Makefile.legacy:9: *** You have legacy configuration in your .config! Please check your configuration.
    88  # BR2_PACKAGE_LINUX_FIRMWARE_ATHEROS_10K_QCA6174=y
    89  BR2_PACKAGE_LINUX_FIRMWARE_ATHEROS_10K_QCA998X=y
    90  BR2_PACKAGE_LINUX_FIRMWARE_ATHEROS_10K_QCA9377=y
    91  BR2_PACKAGE_ZD1211_FIRMWARE=y
    92  
    93  # These packages seem to enable rfkill (and are unnecessary).
    94  # BR2_PACKAGE_CONNMAN is not set
    95  # BR2_PACKAGE_WPA_SUPPLICANT is not set
    96  
    97  # These packages enable SELinux policy.
    98  BR2_PACKAGE_LIBSELINUX=y
    99  BR2_PACKAGE_REFPOLICY=y
   100  BR2_PACKAGE_REFPOLICY_POLICY_STATE_PERMISSIVE=y
   101  # BR2_PACKAGE_REFPOLICY_POLICY_STATE_ENFORCING is not set
   102  # BR2_PACKAGE_REFPOLICY_POLICY_STATE_DISABLED is not set
   103  EOF
   104  
   105  # Per-arch config fragments.
   106  case "$TARGETARCH" in
   107          amd64)
   108  		cat >>.config <<EOF
   109  BR2_TARGET_GENERIC_GETTY_PORT="ttyS0"
   110  BR2_LINUX_KERNEL_USE_ARCH_DEFAULT_CONFIG=y
   111  BR2_LINUX_KERNEL_CONFIG_FRAGMENT_FILES="board/qemu/x86_64/linux.config"
   112  # This is used to create some device links in devfs (see udev rules below),
   113  # but this is too slow for emulated architectures.
   114  BR2_ROOTFS_DEVICE_CREATION_DYNAMIC_EUDEV=y
   115  EOF
   116  ;;
   117          arm64)
   118                  cat >>.config <<EOF
   119  BR2_cortex_a57=y
   120  BR2_LINUX_KERNEL_USE_ARCH_DEFAULT_CONFIG=y
   121  BR2_LINUX_KERNEL_IMAGEGZ=y
   122  BR2_LINUX_KERNEL_GZIP=y
   123  BR2_PACKAGE_HOST_LINUX_HEADERS_CUSTOM_5_10=y
   124  BR2_LINUX_KERNEL_CUSTOM_VERSION_VALUE="5.10"
   125  BR2_ROOTFS_POST_IMAGE_SCRIPT="board/aarch64-efi/post-image.sh ./post_image_script.sh support/scripts/genimage.sh"
   126  BR2_ROOTFS_POST_SCRIPT_ARGS="-c ./custom-genimage-efi.cfg"
   127  EOF
   128  ;;
   129  	arm)
   130  		cat >>.config <<EOF
   131  BR2_cortex_a57=y
   132  # BR2_LINUX_KERNEL is not set
   133  BR2_TARGET_ROOTFS_EXT2_4=y
   134  EOF
   135  ;;
   136  	s390x)
   137  		cat >>.config <<EOF
   138  # BR2_LINUX_KERNEL is not set
   139  EOF
   140  ;;
   141  	mips64le)
   142  		cat >>.config <<EOF
   143  # BR2_LINUX_KERNEL is not set
   144  EOF
   145  ;;
   146  	ppc64le)
   147  		cat >>.config <<EOF
   148  # BR2_LINUX_KERNEL is not set
   149  EOF
   150  ;;
   151  	riscv64)
   152  		cat >>.config <<EOF
   153  # BR2_LINUX_KERNEL is not set
   154  EOF
   155  ;;
   156  esac
   157  
   158  # This script modifies the target root filesystem
   159  # before it's packed into the final image.
   160  # This part is common for all architectures.
   161  cat >rootfs_script.sh <<'EOFEOF'
   162  set -eux
   163  
   164  # Mount debugfs for KCOV and other filesystems.
   165  cat >>$1/etc/fstab <<EOF
   166  debugfs /sys/kernel/debug debugfs defaults 0 0
   167  securityfs /sys/kernel/security securityfs defaults 0 0
   168  configfs /sys/kernel/config/ configfs defaults 0 0
   169  binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc defaults 0 0
   170  smackfs /sys/fs/smackfs smackfs defaults 0 0
   171  selinuxfs /sys/fs/selinux selinuxfs defaults 0 0
   172  fusectl /sys/fs/fuse/connections fusectl defaults 0 0
   173  pstore /sys/fs/pstore pstore defaults 0 0
   174  bpf /sys/fs/bpf bpf defaults 0 0
   175  tracefs /sys/kernel/tracing tracefs defaults 0 0
   176  EOF
   177  
   178  # Setup ssh without key/password.
   179  cat >$1/etc/ssh/sshd_config <<EOF
   180  PermitRootLogin yes
   181  PasswordAuthentication yes
   182  PermitEmptyPasswords yes
   183  ClientAliveInterval 420
   184  Subsystem	sftp	/usr/libexec/sftp-server
   185  EOF
   186  
   187  # Generate sshd host keys.
   188  ssh-keygen -A -f $1
   189  mkdir -p $1/var/db/dhcpcd
   190  
   191  EOFEOF
   192  
   193  # Per-arch part of the rootfs script.
   194  case "$TARGETARCH" in
   195  	amd64)
   196  cat >>rootfs_script.sh <<'EOFEOF'
   197  
   198  # Write udev rules.
   199  cat >$1/etc/udev/rules.d/50-syzkaller.rules <<EOF
   200  ATTR{name}=="vim2m", SYMLINK+="vim2m"
   201  SUBSYSTEMS=="pci", DRIVERS=="i915", SYMLINK+="i915"
   202  EOF
   203  
   204  # Override default grub config with timeout 0.
   205  cat >$1/boot/grub/grub.cfg <<EOF
   206  set default="0"
   207  set timeout="0"
   208  menuentry "syzkaller" {
   209  	linux /boot/bzImage root=/dev/sda1 console=ttyS0
   210  }
   211  EOF
   212  EOFEOF
   213  ;;
   214          arm64)
   215  cat >post_image_script.sh <<'EOFEOF'
   216  cat >${BINARIES_DIR}/efi-part/EFI/BOOT/grub.cfg <<EOF
   217  set default="0"
   218  set timeout="0"
   219  menuentry "syzkaller" {
   220  	linux /Image.gz root=PARTLABEL=root enforcing=0 console=ttyS0
   221  }
   222  EOF
   223  EOFEOF
   224  ;;
   225  esac
   226  
   227  # Adjust consts in buildroot source files.
   228  case "$TARGETARCH" in
   229    arm64)
   230      cp board/aarch64-efi/genimage-efi.cfg custom-genimage-efi.cfg
   231      # 64 MB is too small for our large images.
   232      sed -i 's/size = 64M/size = 256M/g' custom-genimage-efi.cfg
   233      # Also, use compressed images.
   234      sed -i 's/Image/Image.gz/g' custom-genimage-efi.cfg
   235      ;;
   236  esac
   237  
   238  touch post_image_script.sh  # only created for some archs
   239  chmod u+x rootfs_script.sh post_image_script.sh
   240  
   241  make olddefconfig
   242  
   243  if [[ "$NOMAKE" == "" ]]; then
   244  	make
   245  fi