开始学习与Linux kernel相关的东西,第一步就是环境搭建,记录一些错误和解决方法。
参考
参考以下两篇博客:
https://www.anquanke.com/post/id/85837
http://p4nda.top/2018/04/04/kernel-pwn-start/
环境
1 | 系统镜像:ubuntu-14.04-desktop-i386 |
内核编译
1 | sudo apt-get install libncurses5-dev |
错误1
1 | /home/ubuntu/kernel/linux-2.6.32.1/arch/x86/include/asm/ptrace.h:146:13: note: previous declaration of ‘syscall_trace_leave’ was here |
这里的错误是说ptrace.c中syscall_trace_leave函数的定义与ptrace.h中的声明不同:
1 | linux-2.6.32.1/arch/x86/include/asm/ptrace.h |
将ptrace.h中的声明修改为和ptrace.c定义相同即可,另外添加一个头文件:
1 | linux-2.6.32.1/arch/x86/include/asm/ptrace.h |
解决方案参考:https://bestwing.me/Complie-linux-kernel-and-running-it-using-qemu.html
错误2
1 | gcc: error: elf_i386: No such file or directory |
解决方案:
1 | linux-2.6.32.1/arch/x86/vdso/Makefile |
解决方案参考:https://www.anquanke.com/post/id/85837
错误3
1 | drivers/net/igbvf/igbvf.h: At top level: |
修改命名冲突:
1 | linux-2.6.32.1/drivers/net/igbvf/igbvf.h |
解决方案参考:https://www.anquanke.com/post/id/85837
之后执行以下命令过程中,就没有出现错误了。
1 | make all |
busybox
主要是busybox的编译和配置
busybox编译
1 | make menuconfig |
为了避免一些错误,在make menuconfig阶段将一些配置选项修改:
将Linux System Utilities —>中的mkfs_ext2和mkfs_vfat的[*]include改为[ ]exclude,如下图。
将Linux System Utilities —>中的Support mounting NFS file systems on Linux < 2.6.23 (NEW)的[*]include改为[ ]exclude:
将Networking Utilities —>中的inetd (18 kb) (NEW)的[*]include改为[ ]exclude:
将Settings->Build Options->Build static binary(no shared libs)的[ ]exclude改为[*]include:
busybox配置
记录一下配置busybox的命令,首先添加inittab命令。
1 | cd _install |
inittab的内容如下:
1 | #!/bin/sh |
添加rcS文件:
1 | cd _install/etc/ |
rcS文件的内容如下:
1 | #!/bin/sh |
为rcS文件添加可执行权限:
1 | chmod +x rcS |
然后在_install目录下配置dev目录:
1 | mkdir dev |
生成rootfs:
1 | find . | cpio -o --format=newc > ../rootfs.img |
启动qemu:
1 | 32位启动: |
问题1
但是在运行后出现了一个问题,qemu终端显示”can’t open /dev/tty: No such file or directory”,一直以为是busybox配置的问题,后来配置了几遍之后仍然是这个问题,后来查到了解决方案,在配置dev目录时需要再添加一句命令:
1 | sudo mknod /dev/tty1 c 4 1 |
解决方案参考:https://www.linuxquestions.org/questions/linux-software-2/dev-tty-no-such-file-or-directory-889996/
问题2
再次启动qemu后,出现以下问题:
查找资料,找到解决方法,编辑_install/etc/目录下的inittab文件:
1 | 将::askfirst:/bin/ash |
关闭canary
后面再做题的时候需要关闭canary,因此需要将内核重新编译,修改内核的配置文件.config,将下列语句注释掉:
1 | #./linux-2.6.32.1/.config |
重新编译:
1 | make |