I might try to teach myself kernel hacking soon, specifically TCP.
Does anyone have any suggestions where to start and what to read?
I know C, but so far only in user space.
@NerdResa I think I recall reading a book by Comer about Unix which was quite good:
https://www.theregister.co.uk/2018/03/27/microsoft_wsl_oss/
He wrote about TCP/IP too.
That's probably a bit old though.
@mmu_man Does the book say anything about how to hack a Linux kernel?
@NerdResa nope, it's more general on the usual Unix architecture.
@XavCC Thanks, I'll check it out. I even speak French, but tech documentation in French would be a new thing for me.
@mmu_man Ok, that won't help with Linux kernel hacking. Thanks for trying though.
@NerdResa well, you didn't specify which kernel :p
Besides, still useful to read a bit about the theory as well.
@mmu_man @NerdResa Yup. The general idea from the book will probably be still there, but you will find a lot of optimisations (offloads, message-something to avoid lots of irqs under high load, async workers etc). You should also be aware that this is one of „tightly” maintained subsystems and maintainers have a very clear vision of it, if you know what I mean.
@NerdResa I don't have much experience, but if you want to get closer to the kernel you might want to try anything related to system programming.
More specifically you can train yourself with Linux From Scratch:
http://www.linuxfromscratch.org/
Then once you're experienced and familiar enough with GNU/Linux' internals (or if it's already the case), you can probably get into the real thing here :
https://kernelnewbies.org/
Good luck!
@Neil I've done low-level user space on Linux, e.g. stuff with netlink. Don't know yet how far the "jump" to the kernel is.
Thanks for the resources, I'll check them out.
Should have specified that:
I'm interested to hack the Linux kernel.
I've done low-level user space hacking in C on Linux. Now wondering how much additional knowledge I need for Linux kernel stuff…
@NerdResa you need to know there's no hope
@NerdResa The Linux Device Drivers book is available online under Creative Commons and works for many people as a way to get started:
There are repositories on GitHub with the examples updated to newer kernel versions, e.g.
https://github.com/martinezjavier/ldd3
My advice would be to find some area that interests you or an itch to scratch and read a lot of code. I started with the crypto subsystem (Ripemd algo) and the code got merge surprisingly quickly :p
@Kensan Thanks, I'll take a look at it!
My area is Networking, specifically TCP. (Don't worry, I'm not trying to get this upstream)
@NerdResa @Kensan Unfortunately the Linux Device Drivers book covers very little of the networking stack, because it's focused on things developers need to know to write… well, device drivers. 😅 But the first eight chapters may have good context for Linux kernel hacking in general, and the network drivers chapter should at least cover some of the abstractions that are shared with the higher layers.
@NerdResa @Kensan from the theory side, i suggest https://www.cl.cam.ac.uk/~pes20/Netsem/paper3.pdf and the full label transition system https://www.cl.cam.ac.uk/~pes20/Netsem/alldoc.pdf -- but this doesn't contain anything about kernel programming, just tcp/ip.. ;)
@NerdResa You probably don't need anything more to start hacking. You will see, however, a lot of kernel specific programming patterns and utilities: lists, rcu, offsetof, macros (tons of them) etc. Always look at two implementations of object of the same class like TCP and SCTP to find patterns in the interfacing code.
@steelman That sounds helpful, thanks! :)
@NerdResa if you have an idea of what you want to hack on just jump in and try to make it work. Enjoy the discovery, you can worry about doing-things-the-right-way-TM later...
Ofc things may go horribly wrong, but it's easier to find help with a practical problem later on :)
@NerdResa hm maybe https://mobile.twitter.com/alicegoldfuss can give some advice. Afaik she's starting in the topic as well.
@NerdResa The best introductory/tutorial material I'm aware of for getting started on Linux kernel hacking is the first-patch documentation for Outreachy internship applicants: https://kernelnewbies.org/OutreachyfirstpatchSetup
Most of the Linux kernel is plain C, with some caveats: 1) Linux kernel developers often use obscure corners of the language standard as well as GCC/clang-specific extensions; 2) the standard library is not available, so e.g. there's kmalloc not malloc, etc.
@NerdResa Also you may find https://www.kernel.org/doc/ helpful as reference material. Alternatively you can find a fair amount of the same material in your kernel source tree checkout under the Documentation/ directory. I often use `git grep` on either the Documentation/ or include/ directory when I see a function I don't understand.
@jamey Oooh, I was looking for something like this! Thank you *-*
@NerdResa There's some amount of material you'll need to learn but depending on what you want to do it might not be all that much. A lot of kernel stuff has analogies in userspace, just with different names.
It helps to do some reading about fundamental kernel concepts before diving into code. I have some slightly outdated linux 2.6-ish kernel development books collecting dust on my shelf which I could drop off somewhere at TU for you if you want.
@stsp Yea, I think the thing I'm planning to do isn't actually that much, but I'm not sure yet. Usually I'd read a book but I don't think I've got enough time, I'll just start hacking. :)
Thanks for the offer though, I might get back to it.
@NerdResa Sure, any time. Happy hacking 🙃
@NerdResa What exactly do you want to do? TCP as in the network protocol? I would begin with understanding the Linux Kernel (there are books from O'Reilly I believe, which are really good, but about Linux 2.6). Knowledge of UNIX and POSIX is also appreciated.
@NerdResa If you want to gather experiences in bare-metal programming (i.e. OS development), grab yourself either QEMU or Raspberry Pi or a board, which is in fact documented well, and write stuff. The really interesting part for me is putting everything together, that already works in Linux :D
@snaums TCP the transport protocol, yes. I'm not aware of any other TCP there is in the kernel ;>
@NerdResa I'd like to know as well! o/
@NerdResa understand how the protocol works. Comer or Stevens are ok but a bit dated.
Have a look at the networking part of the kernel, possibly BSD as well for comparison.
I found the raw sockets interesting for seeing how stuff arrived. It is also possibly what you would use for sending malformed packets.
Attacking the network stack you'd be probably writing in userspace anyhow though possibly with privileged levels.
@NerdResa
I'd read Tickets, code and discussions on tickets
@NerdResa
At the HAW Hamburg we lerned about kernel space stuff by writing driver kernel modules
https://bootlin.com/doc/books/ldd3.pdf
@NerdResa good luck