Latest / CyberSecurity Summary / Windows Kernel Programming
Transcript
- 0:00Speaker 1: All right, jumping right into this deep dive Windows kernel programming.
- 0:04Speaker 1: Oh yeah, you handed me some excerpts from Pavel Yosefovic's book,
- 0:10Speaker 1: and I gotta say, some of this stuff intense. Yeah,
- 0:13Speaker 1: it's intense. It's a little over my head. Yeh, that's
- 0:15Speaker 1: why you're here, thankfully. But I'm ready to learn, ready
- 0:18Speaker 1: to uncover the secrets of the kernel some most people
- 0:21Speaker 1: never even you know, think about.
- 0:23Speaker 2: Well, Yusefavich definitely doesn't shy away from the complexity of it.
- 0:29Speaker 2: We're talking processes, threads, virtual memory system calls, object managers, handles,
- 0:35Speaker 2: I mean, the whole nine yards, and even getting into
- 0:38Speaker 2: like setting up a kernel development environment, and you know,
- 0:41Speaker 2: the nuances of programming at the kernel level.
- 0:43Speaker 1: Yeah, I skim those sections, and let's just say, I'm
- 0:46Speaker 1: glad you're the one fluent in kernels. But before we
- 0:49Speaker 1: get too lost in the weeds, here something that caught
- 0:51Speaker 1: my eye. File system minifilters. Yeah, Yusefovich talks about them
- 0:55Speaker 1: like there's some kind of secret weapon for developers.
- 0:58Speaker 2: They kind of are. You know, it's amazing what you
- 1:01Speaker 2: can do with a minifilter. I mean, think about it
- 1:02Speaker 2: this way, like a minifilter can actually step in and
- 1:06Speaker 2: intercept filesystem operations, yeah, before they even get to the
- 1:09Speaker 2: file system itself.
- 1:10Speaker 1: So like, if I'm an anti virus program, I can
- 1:13Speaker 1: use a minifilter to scan a file before it even
- 1:15Speaker 1: gets written to the disc.
- 1:16Speaker 2: That's one example. Yeah, yeah, but it goes way beyond that, right, Like,
- 1:20Speaker 2: you can use it for everything from real time backups
- 1:22Speaker 2: to like really granular access control. Yasufovich even gives an
- 1:26Speaker 2: example of a minifilter that encrypts files on the fly,
- 1:30Speaker 2: completely transparent to the user.
- 1:32Speaker 1: Wow. Yeah, that's pretty mind blowing.
- 1:34Speaker 2: Yeah.
- 1:35Speaker 1: Okay, so before we get too far ahead of ourselves,
- 1:37Speaker 1: let's rewind a bit. Yu Seefavitch spends a good chunk
- 1:40Speaker 1: of the book talking about processes and threads, you know,
- 1:44Speaker 1: the building blocks of everything that happens in the kernel.
- 1:46Speaker 1: It's been a while since I've had to think about
- 1:48Speaker 1: that stuff. Remind me, why are they so important?
- 1:51Speaker 2: Well, think of a process like a container, right, Like
- 1:54Speaker 2: it's a little world into itself. It has its own
- 1:57Speaker 2: memory space, it has its own security set, it has
- 2:00Speaker 2: its own set of resources. And then the threads are
- 2:03Speaker 2: like the workers within that container that actually carry out
- 2:06Speaker 2: the instructions.
- 2:07Speaker 1: Okay, that makes sense. But Yusifavich mentioned something kind of interesting.
- 2:11Speaker 1: He says that a process without any threads is basically useless.
- 2:16Speaker 1: Why is that, Well.
- 2:18Speaker 2: It's like having you know, all the equipment in a factory,
- 2:21Speaker 2: but no workers to run the equipment. Right, Like, without threads,
- 2:24Speaker 2: a process can actually do anything. It's just taking up space.
- 2:27Speaker 2: So the kernel, being you know, the efficient manager it is,
- 2:31Speaker 2: it'll eventually destroy it.
- 2:32Speaker 1: Ah. So it's all about making sure resources are being
- 2:35Speaker 1: used effectively.
- 2:36Speaker 2: Yeah, okay.
- 2:37Speaker 1: Now another thing that always kind of trips me up
- 2:39Speaker 1: is virtual memory. You know, it's like this magical realm
- 2:41Speaker 1: where every process thinks it has all the memory in
- 2:44Speaker 1: the world, even if it's sharing a physical machine with
- 2:46Speaker 1: a bunch of other processes. How does that even work?
- 2:49Speaker 2: Well, Yussefhavich describes it as, you know, like each process
- 2:53Speaker 2: gets its own map to a vast territory, but only
- 2:56Speaker 2: the areas that they actually use get real Okay, Right.
- 2:59Speaker 2: The kernel is like the master cartographers, right, it's managing
- 3:03Speaker 2: all these maps making sure that they don't overlap.
- 3:05Speaker 1: Okay, that's a helpful way to think about it. So
- 3:08Speaker 1: the kernel is constantly juggling all these virtual maps, making
- 3:12Speaker 1: sure that each process has the illusion of its own
- 3:15Speaker 1: private memory space. But what happens when a process needs
- 3:19Speaker 1: to do something that requires like kernel privileges, you know,
- 3:23Speaker 1: like access hardware, modify systems settings. That's where system calls.
- 3:27Speaker 2: Come in, right, Yeah, exactly. So it's like needing to
- 3:30Speaker 2: go through customs, you know, to cross a border. Like
- 3:33Speaker 2: a process can't just waltz into kernel mode whenever it wants.
- 3:36Speaker 2: It needs to present the right credentials, follow the protocol.
- 3:39Speaker 1: And you see, Fitch gets into a really in depth
- 3:41Speaker 1: detail about how this works at like the low level.
- 3:43Speaker 1: He talks about system call numbers and the system service
- 3:46Speaker 1: dispatch table and all sorts of other kind of intricate mechanisms.
- 3:50Speaker 1: Is there anything in particular about system calls that stood
- 3:53Speaker 1: out to you, you know, anything that surprised you or
- 3:55Speaker 1: made you think differently about how they work?
- 3:57Speaker 2: You Know what really struck me is just the level
- 4:01Speaker 2: of indirection involved. Yeah, you know what I mean. Like,
- 4:03Speaker 2: it's not just a simple matter of a user mode
- 4:07Speaker 2: function calling a kernel mode function directly, right. There are
- 4:11Speaker 2: layers upon layers of abstraction, like checks and balances to
- 4:15Speaker 2: ensure security and stability.
- 4:17Speaker 1: So even something as seemingly straightforward as a system call
- 4:20Speaker 1: is actually this really intricate dance between user mode and
- 4:23Speaker 1: kernel mode. It's fascinating but also a little daunting, to
- 4:27Speaker 1: be honest.
- 4:28Speaker 2: Well, yeah, I mean that's the kernel for you, right,
- 4:30Speaker 2: It's a world of complexity and power, and you know
- 4:33Speaker 2: where even small mistakes can have huge consequences. But you know,
- 4:38Speaker 2: that's also what makes it so intriguing. There's always more
- 4:40Speaker 2: to learn, there's always more to discover.
- 4:42Speaker 1: I'm starting to feel like an explorer setting off into
- 4:44Speaker 1: uncharted territory. Speaking of territory, Yosefovich mentions this thing called
- 4:49Speaker 1: the object manager, which he describes as like the kernel's
- 4:53Speaker 1: internal librarian. What's that all about?
- 4:56Speaker 2: Yeah, so the object manager is essentially the kernel's way
- 4:59Speaker 2: of keeping track of air everything, files, processes, threads, devices,
- 5:03Speaker 2: you name it. It's like this vast library, with every
- 5:07Speaker 2: resource neatly cataloged and organized.
- 5:09Speaker 1: So even the kernel needs a good filing system, that's right,
- 5:13Speaker 1: But what's the point of having this object manager? Why
- 5:15Speaker 1: not just let each component manage its own resources.
- 5:18Speaker 2: Well, centralizing resource management this way through the object manager
- 5:22Speaker 2: has a lot of benefits, right. It enforces consistency, it
- 5:25Speaker 2: prevents conflicts, it makes it much easier to track an
- 5:28Speaker 2: audit resource usage, and plus it provides kind of this
- 5:32Speaker 2: uniform interface for accessing and manipulating resources regardless of their type.
- 5:37Speaker 1: That makes sense. It's like having a single point of
- 5:40Speaker 1: contact for all your resource needs rather than having to
- 5:42Speaker 1: deal with a bunch of different departments. Right, But how
- 5:44Speaker 1: do user mode processes actually interact with these objects that
- 5:48Speaker 1: are managed by the kernel.
- 5:49Speaker 2: Ah, that's where handles come in. Okay, so Yosipovic describes
- 5:53Speaker 2: these handles as like tickets that user mode programs use
- 5:57Speaker 2: to access those kernel objects. It's like getting a claim
- 6:01Speaker 2: ticket at a coat check, right, Yeah, you present your
- 6:03Speaker 2: ticket to retrieve your coat.
- 6:05Speaker 1: So a handle is basically a reference to an object
- 6:08Speaker 1: that's being managed by the kernel. Yes, but Yusufhovich also
- 6:11Speaker 1: mentions that each process has its own private handle table.
- 6:14Speaker 2: Why is that, Well, it's all about security and isolation, right.
- 6:18Speaker 2: By giving each process its own handle table, the kernel
- 6:21Speaker 2: can control exactly which processes have access to which objects,
- 6:26Speaker 2: and it can also prevent one process from messing with
- 6:28Speaker 2: another processes objects, even if they happen to have the
- 6:32Speaker 2: same handle value.
- 6:33Speaker 1: Right, So it's like each process gets its own set
- 6:35Speaker 1: of keys to access the kernel's resources, and those keys
- 6:38Speaker 1: don't work for any other process exactly. But Yusufhovich also
- 6:41Speaker 1: points out something kind of interesting. He says that the
- 6:44Speaker 1: name that's displayed for a handle can have different meanings
- 6:46Speaker 1: depending on the object type. What's that all about.
- 6:49Speaker 2: It can be a little confusing at first, but it
- 6:51Speaker 2: actually makes a lot of sense when you think about it.
- 6:54Speaker 2: You know, for some object types like mutex is, the
- 6:57Speaker 2: handle name is simply the name of the objects. Yeah,
- 7:00Speaker 2: But for other object types like files and directories, the
- 7:04Speaker 2: handle name is actually the path to that object in
- 7:07Speaker 2: the file system.
- 7:08Speaker 1: Okay, so the handle name isn't always a literal name.
- 7:11Speaker 1: It can also be like a path or some other
- 7:15Speaker 1: kind of identifier that's meaningful for that specific object type.
- 7:19Speaker 2: That's right.
- 7:19Speaker 1: But all this talk about handles and objects in kernel
- 7:23Speaker 1: mode is making me really curious about what it actually
- 7:25Speaker 1: takes to write code that runs in this kind of
- 7:28Speaker 1: privileged realm. Yusifovich has a whole section on setting up
- 7:32Speaker 1: a kernel development environment, and let's just say it looks
- 7:34Speaker 1: a lot different from my usual user mode coding setup.
- 7:37Speaker 1: Oh yeah, it's a whole different world down there, it is.
- 7:40Speaker 2: You need special tools like the Windows Driver kit. You
- 7:43Speaker 2: need a debugger they can understand what's going on in
- 7:45Speaker 2: the kernel, and you need a healthy dase of patients.
- 7:48Speaker 1: Yusifovich mentions the importance of error handling in Kernal development.
- 7:53Speaker 1: He says, even small mistakes can bring down the entire system.
- 7:58Speaker 1: Why is that what makes kernel development so much more
- 8:01Speaker 1: unforgiving than user mode programming. Yeah.
- 8:04Speaker 2: Well, in user mode, if your code crashes, it usually
- 8:06Speaker 2: only affects the current process, right, the rest of the
- 8:08Speaker 2: system kind of keeps humming along. But in kernel mode,
- 8:11Speaker 2: your code's running in the same address space as the
- 8:14Speaker 2: operating system itself, So a bug in your driver can
- 8:18Speaker 2: actually corrupt critical data structures, you know, leading to system
- 8:22Speaker 2: instability or even a complete crash.
- 8:24Speaker 1: So it's like playing with live wires.
- 8:27Speaker 2: Yeah, pretty much.
- 8:28Speaker 1: One wrong move and the whole house goes dark.
- 8:30Speaker 2: Yeah, exactly. And to make things even more challenging, kernel
- 8:34Speaker 2: development often involves like working with undocumented features reverse engineering
- 8:39Speaker 2: existing drivers.
- 8:40Speaker 1: Oh wow.
- 8:41Speaker 2: Yeah, so it's not for the faint of heart.
- 8:42Speaker 1: Yeah, I'm sarrying to understand why kernel developers are often
- 8:45Speaker 1: seen as like wizards or gurus. But let's dive into
- 8:48Speaker 1: some of the specifics of kernel programming. Here. Yasifavich talks
- 8:52Speaker 1: about different memory pools that drivers can use, paged and
- 8:56Speaker 1: non paged. What's the difference between these pools and why
- 8:58Speaker 1: does it matter?
- 9:00Speaker 2: Okay, so think about it this way. Page pool memory
- 9:03Speaker 2: is kind of like this, you know, flexible workspace that
- 9:06Speaker 2: can be moved around as needed. Okay, right, So it
- 9:09Speaker 2: could be swapped out to disk if the system is
- 9:12Speaker 2: running low on physical memory and brought back in when
- 9:14Speaker 2: it's needed again. But non paged pool memory, on the
- 9:18Speaker 2: other hand, that's like your you know, dedicated office space.
- 9:22Speaker 2: It's always available.
- 9:23Speaker 1: So nontage memory is like the VIP section. Yes, always
- 9:27Speaker 1: guarantee it a spot in RAM exactly. But wouldn't having
- 9:30Speaker 1: everything be non paged be a recipe for disaster?
- 9:33Speaker 2: You're right, You're absolutely right, and that's why drivers have
- 9:35Speaker 2: to be really, really careful about how they use non
- 9:37Speaker 2: paged memory. It's a precious resource that should only be
- 9:40Speaker 2: used for you know, code or data that absolutely cannot
- 9:43Speaker 2: be paged out, things like interrupt handlers or data structures
- 9:46Speaker 2: that need to be accessed very quickly and reliably.
- 9:48Speaker 1: It's all about striking that balance between performance and resource utilization. Yeah, okay,
- 9:54Speaker 1: so how does a driver actually tell the operating system
- 9:58Speaker 1: what it can do? Is there some kind of registration process?
- 10:01Speaker 2: There is? Yeah, So each driver has this special data
- 10:05Speaker 2: structure called a driver object. It's kind of like the
- 10:08Speaker 2: driver's resume right outlining its capabilities, and now it interfaces
- 10:11Speaker 2: with the rest of the system.
- 10:12Speaker 1: Interesting, So what kind of information is stored in this
- 10:15Speaker 1: driver object? What does the kernel need to know about
- 10:18Speaker 1: a driver in order for it to function properly.
- 10:21Speaker 2: Well, it's packed with important details. The driver object includes
- 10:26Speaker 2: things like the driver's name, the device object that it's
- 10:29Speaker 2: associated with, and probably most importantly, a list of function
- 10:33Speaker 2: pointers called the major function array. Okay, and this array
- 10:37Speaker 2: tells the operating system what IO requests the driver can handle.
- 10:41Speaker 1: So it's kind of like a menu of services that
- 10:43Speaker 1: the driver offers, like I can create files, I can
- 10:46Speaker 1: read files, I can delete files.
- 10:47Speaker 2: That's exactly right. Yeah. Each entry in that major function
- 10:50Speaker 2: array points to a specific function within the driver that's
- 10:55Speaker 2: responsible for handling a particular type of IO request. So,
- 11:00Speaker 2: for example, if the operating system needs to create a
- 11:02Speaker 2: new file, It'll check the driver's major function array for
- 11:06Speaker 2: an entry that corresponds to file creation.
- 11:09Speaker 1: Requests, and if the driver has registered a handler for
- 11:12Speaker 1: that request type, then the operating system will call that function,
- 11:16Speaker 1: passing along all the necessary information about the requests.
- 11:18Speaker 2: You got it. And that's where things get really interesting, right, Okay,
- 11:21Speaker 2: because those io requests are packaged up in these things
- 11:24Speaker 2: called IRPs iorequest packets, and Yosefovich really gets into the
- 11:30Speaker 2: nitty gritty of how these IRPs work.
- 11:33Speaker 1: Yeah, I remember him describing IRPs as like the language
- 11:36Speaker 1: of the kernel, the way that all the different components
- 11:38Speaker 1: communicate with each other. Right, But if I'm being honest,
- 11:41Speaker 1: the whole concept of IRP still feels a bit abstract
- 11:44Speaker 1: to me. Can you break it down for me? What
- 11:46Speaker 1: do these IRPs actually look like and how do they
- 11:50Speaker 1: flow through the system?
- 11:52Speaker 2: Okay? So imagine an IRP as an envelope, okay, right,
- 11:56Speaker 2: and inside this envelope is all sorts of information about
- 12:00Speaker 2: the request and what type of operation it is, what
- 12:03Speaker 2: file or device it's targeting, any data that needs to
- 12:06Speaker 2: be transferred, and so on.
- 12:07Speaker 1: So it's like a standardized form that everyone agrees on
- 12:10Speaker 1: just making sure that all the necessary information is included.
- 12:12Speaker 2: Precisely, and these IRPs, they get passed around from one
- 12:15Speaker 2: component to another as the request is processed. So, for example,
- 12:19Speaker 2: if a user mode application wants to write to a file,
- 12:23Speaker 2: it'll make a system call, which will eventually result in
- 12:26Speaker 2: an IRP being created in the kernel, and that IRP
- 12:29Speaker 2: might be passed through several layers of drivers, each one
- 12:32Speaker 2: potentially examining or modifying the request before it finally reaches
- 12:37Speaker 2: the filesystem driver that's responsible for actually writing the data
- 12:40Speaker 2: to disk.
- 12:40Speaker 1: So it's kind of like a relay race, with the
- 12:42Speaker 1: IRP being passed from one runner to the next until
- 12:45Speaker 1: it reaches the finish nine exactly. But with all these
- 12:47Speaker 1: different components potentially handling the IRP, how does the system
- 12:51Speaker 1: make sure that everything happens in the right order and
- 12:54Speaker 1: that nothing gets lost or corrupted along the way.
- 12:57Speaker 2: Well that's where the io manager comes in, okay, right,
- 12:59Speaker 2: It's like the racecord making sure all the runners are
- 13:01Speaker 2: in their lanes and the baton gets passed smoothly. So
- 13:04Speaker 2: the iron manager keeps track of all the IRPs in
- 13:06Speaker 2: the system, it routes them to the appropriate drivers. Yeah,
- 13:11Speaker 2: and it manages things like buffering and completion routines.
- 13:14Speaker 1: It's a complex system, but it's pretty amazing how it
- 13:18Speaker 1: all works together to handle, you know, potentially millions of
- 13:21Speaker 1: these io requests every second.
- 13:23Speaker 2: Yeah.
- 13:24Speaker 1: But let's go back to those mini filters we talked
- 13:26Speaker 1: about earlier. Where do they fit into this whole IRP
- 13:29Speaker 1: processing pipeline.
- 13:31Speaker 2: Ah? Yes, So mini filters are like secret agents that
- 13:34Speaker 2: can intercept those IRPs at various points along their journey.
- 13:38Speaker 1: So they can kind of peek inside the envelope. Yeah,
- 13:41Speaker 1: maybe even slip in a little something extra before passing
- 13:44Speaker 1: it along to the next component.
- 13:45Speaker 2: That's a great way to think about it. And that
- 13:47Speaker 2: ability to intercept and manipulate those IRPs is really what
- 13:52Speaker 2: makes minifilters so incredible powerful. They can, you know, they
- 13:54Speaker 2: can filter, they can modify, redirect, they can even block
- 13:57Speaker 2: IOA requests, all without the user even knowing it's happening.
- 14:01Speaker 1: Right. We talked earlier about antivirus programs using minifilters to
- 14:06Speaker 1: scan files before they're written to the disc, but Yosefavic
- 14:10Speaker 1: mentions a whole range of other applications like back up
- 14:13Speaker 1: and restore data encryption, even performance optimization. It seems like
- 14:17Speaker 1: there's a lot you can do with these things.
- 14:19Speaker 2: The possibilities are really endless, you know. For example, you
- 14:22Speaker 2: could use a mini filter to create a shadow copy
- 14:26Speaker 2: of every file that's written to a specific directory, right, Yeah,
- 14:28Speaker 2: essentially creating a real time backup.
- 14:30Speaker 1: That's impressive. And I remember Yusufovich mentioning something about minifilters
- 14:35Speaker 1: being able to enforce like really granular access control. Yes,
- 14:39Speaker 1: how does that work?
- 14:40Speaker 2: So imagine you want to restrict access to certain files
- 14:43Speaker 2: based on the user's identity, you know, or group membership.
- 14:46Speaker 2: A mini filter can actually examine the IERP for every
- 14:50Speaker 2: file access request, check the user's credentials, and if the
- 14:54Speaker 2: user doesn't have the necessary permissions, the minifilter can simply
- 14:58Speaker 2: block the requests.
- 15:00Speaker 1: So like having a bouncer at the door of every
- 15:02Speaker 1: file checking IDs and making sure that only authorized personnel
- 15:06Speaker 1: get through.
- 15:06Speaker 2: That's a great way to put it.
- 15:07Speaker 1: But with so much power at their disposal, isn't there
- 15:10Speaker 1: a risk that many filters could interfere with each other, right,
- 15:13Speaker 1: or even with the normal operation of the system.
- 15:16Speaker 2: Yeah, that's a valid concern, but the kernel's designed to
- 15:19Speaker 2: manage these interactions very carefully. So MANI filters are assigned
- 15:23Speaker 2: in altitude, which determines their order in the filtering chain altitude.
- 15:27Speaker 1: So it's like stacking them on top of each other, Yes,
- 15:30Speaker 1: with the higher altitude filters getting to see the IERP first.
- 15:33Speaker 2: That's exactly right. And this altitude system is carefully managed
- 15:38Speaker 2: to prevent those kinds of conflicts. In fact, Yosiphovich emphasizes it,
- 15:42Speaker 2: if you want to develop a production grade minifilter, you
- 15:45Speaker 2: actually need to a pain of proper altitude allocation for
- 15:47Speaker 2: Microsoft really, so.
- 15:48Speaker 1: It's not just a free for all where any developer
- 15:51Speaker 1: can choose any altitude they want, not at all.
- 15:53Speaker 2: No, Microsoft has these established ranges of altitudes for different purposes. Right,
- 16:00Speaker 2: Virus filters might occupy one altitude range while backup filters
- 16:04Speaker 2: occupy another.
- 16:05Speaker 1: Okay, that makes sense. It's like air traffic control, right,
- 16:07Speaker 1: making sure that all the planes are flying at the
- 16:09Speaker 1: right altitudes to prevent collisions. But this whole altitude allocation
- 16:14Speaker 1: process sounds pretty involved. Why is it so important?
- 16:17Speaker 2: Well, imagine what could happen if two minifilters with like
- 16:20Speaker 2: incompatible goals were operating at the same altitude.
- 16:24Speaker 1: Let me guess chaos chaos exactly.
- 16:28Speaker 2: You know, one minifilter might be trying to block access
- 16:30Speaker 2: to a file while the other one's trying to allow access.
- 16:33Speaker 2: Or one minifilter might be modifying the data in an
- 16:36Speaker 2: IRP while the other is expecting that data to be unchanged.
- 16:40Speaker 1: It sounds like a recipe for disaster.
- 16:42Speaker 2: Yeah, So this altitude allocation process is really all about
- 16:45Speaker 2: ensuring that mini filters can coexist peacefully and that the
- 16:48Speaker 2: system remains stable and predictable.
- 16:50Speaker 1: So this altitude allocation process is all about ensuring that
- 16:54Speaker 1: mani filters can coexist peacefully and that the system remains
- 16:57Speaker 1: stable and predictable. But let's dive a bit deeper into
- 17:00Speaker 1: the actual structure of a minifilter. How are they implemented
- 17:04Speaker 1: and what are the key components that developers need to understand? Okay,
- 17:08Speaker 1: from what I gathered from Yosifovic, a minifilter is essentially
- 17:12Speaker 1: a driver that registers a set of callback functions with
- 17:15Speaker 1: the filter manager.
- 17:17Speaker 2: Right, and those callback functions are the minifilter's eyes and ears, right, Okay,
- 17:21Speaker 2: allowing you to to observe and interact with IRPs as
- 17:24Speaker 2: they flow through the system.
- 17:26Speaker 1: He mentions two main types of callbacks, pre operation and
- 17:29Speaker 1: post operation. What's the difference between those and why are
- 17:33Speaker 1: they important?
- 17:33Speaker 2: Okay, so imagine you're standing at a checkpoint on a road,
- 17:37Speaker 2: right right, A pre operation callback is like checking the
- 17:40Speaker 2: driver's license and registration before the car is allowed to
- 17:42Speaker 2: pass through, and then a post operation callback is like
- 17:45Speaker 2: checking the trunk after the cars pass through, just to
- 17:48Speaker 2: make sure everything's in order.
- 17:49Speaker 1: So a pre operation callback gives the minifilter a chance
- 17:52Speaker 1: to examine and potentially modify the IRP before the operation
- 17:57Speaker 1: is actually performed, right, while a post operation callback gives
- 18:00Speaker 1: it a chance to inspect the results of the operation
- 18:04Speaker 1: and take any necessary actions based on.
- 18:06Speaker 2: Those results exactly. And those callbacks give mini filters just
- 18:10Speaker 2: this incredible amount of flexibility, right. They can use pre
- 18:13Speaker 2: operation callbacks to block requests, to modify data, to even
- 18:17Speaker 2: redirect the IRP to a different device or file. And
- 18:20Speaker 2: they can use post operation callbacks to log events, to
- 18:24Speaker 2: clean up resources, or even to initiate new io requests.
- 18:28Speaker 1: It's like having checkpoints at every stage of the journey,
- 18:31Speaker 1: allowing the mini filter to kind of control the flow
- 18:33Speaker 1: of traffic and make sure that everything is running smoothly,
- 18:36Speaker 1: that's right. But Yasifovich also mentioned something called contexts in
- 18:39Speaker 1: the context of mini filters. What are those?
- 18:42Speaker 2: Okay? So, contexts are kind of like little sticky notes
- 18:46Speaker 2: that a minifilter can attach to to various objects in
- 18:49Speaker 2: the kernel, you know, yeah, like files, streams, or even volumes.
- 18:55Speaker 2: They're a way for the mini filter to store information
- 18:58Speaker 2: that's specific to that object, you know, like flags or
- 19:01Speaker 2: counters or even cash data.
- 19:04Speaker 1: So it's like adding a little bit of extra information
- 19:07Speaker 1: to the object. Yeah, information that's only visible to the minifilter,
- 19:10Speaker 1: that's right.
- 19:11Speaker 2: And those context can be incredibly useful for managing state
- 19:15Speaker 2: and sharing information between different callback functions within the minifilter.
- 19:19Speaker 1: It sounds like a clever way to kind of keep
- 19:21Speaker 1: track of things right and avoid having to store state
- 19:24Speaker 1: information globally, which could potentially lead to you know, conflicts
- 19:28Speaker 1: or race conditions exactly. But let's talk about something that's
- 19:31Speaker 1: that's always on my mind when it comes to kernel development,
- 19:33Speaker 1: error handling. Yusuphovic stresses the importance of failing fast and
- 19:38Speaker 1: failing safely in kernel mode. Yes, why is that so crucial?
- 19:41Speaker 2: Well, remember, in kernel mode you're operating in the same
- 19:45Speaker 2: address space as the operating system itself. Ah Right, So
- 19:49Speaker 2: even a seemingly minor error in your driver can have
- 19:52Speaker 2: catastrophic consequences.
- 19:54Speaker 1: Right, It's like playing with live wires. One wrong move
- 19:57Speaker 1: and the whole house goes dart exactly.
- 19:59Speaker 2: So if you're driver encounters an unexpected condition, it's essential
- 20:03Speaker 2: to stop processing their request immediately and return an appropriate
- 20:07Speaker 2: error code.
- 20:07Speaker 1: So it's better to abort the mission than to risk
- 20:11Speaker 1: causing more damage.
- 20:12Speaker 2: Absolutely, and just as important as failing fast is failing safely.
- 20:18Speaker 2: So this means ensuring that your driver doesn't leave the
- 20:21Speaker 2: system in an inconsistent state even if an error does occur.
- 20:25Speaker 2: All resources need to be properly released, all data structures
- 20:28Speaker 2: need to be cleaned.
- 20:28Speaker 1: Up, so it's like making sure you lock the doors
- 20:31Speaker 1: and turn off the lights before you leave the house,
- 20:33Speaker 1: even if you're rushing out in an emergency.
- 20:34Speaker 2: Precisely, and Yosefovich highlights several techniques that drivers can use
- 20:39Speaker 2: to ensure safe air handling, like using tri finally blocks
- 20:42Speaker 2: to guarantee that cleanup code is executed even if an
- 20:45Speaker 2: exception is thrown.
- 20:46Speaker 1: It sounds like AerR handling is serious business. In kernel development,
- 20:50Speaker 1: it is. But let's move on to another kind of
- 20:52Speaker 1: challenging aspect here, debugging. We talked earlier about how kernel
- 20:56Speaker 1: debugging requires these specialized tools and techniques, right, but is
- 21:00Speaker 1: there anything specific to mini filter debugging that developers should
- 21:04Speaker 1: be aware of.
- 21:05Speaker 2: Yeah. So one of the biggest challenges is that minifilters
- 21:08Speaker 2: often operate at a very low level, right, Yeah, interacting
- 21:12Speaker 2: with the filesystem and other kernel components in ways that
- 21:14Speaker 2: can be difficult to observe from user mode.
- 21:17Speaker 1: So it's like trying to debug a program that's running
- 21:20Speaker 1: on a different planet.
- 21:22Speaker 2: In a way. Yeah, But fortunately there are tools that
- 21:25Speaker 2: can help Yasifovich mentions a special debugger expression called fltkd
- 21:29Speaker 2: dot dll, which provides commands that are specifically designed for
- 21:33Speaker 2: debugging minifilters, so you can use it to display information
- 21:36Speaker 2: about loaded minifilters, their instances, attached volumes, even their internal state.
- 21:42Speaker 1: It's like having x ray vision into the world of
- 21:44Speaker 1: minifilters exactly.
- 21:46Speaker 2: And there are other techniques like using kernel mode logging
- 21:50Speaker 2: or carefully crafted test cases to try to isolate and
- 21:53Speaker 2: reproduce issues.
- 21:54Speaker 1: It sounds like debugging mini filters requires a special blend
- 21:57Speaker 1: of patients, persistence and creativity.
- 22:00Speaker 2: It definitely does. But for those who dare to venture
- 22:03Speaker 2: into this realm, the rewards can be great. There's nothing
- 22:06Speaker 2: quite like the feeling of tracking down a tricky bug
- 22:09Speaker 2: in a minifilter and knowing that you've made the system
- 22:12Speaker 2: a little bit more stable, a little bit more secure.
- 22:14Speaker 1: It's like solving a puzzle, except the pieces are scattered
- 22:17Speaker 1: throughout the kernel, right, and the solution could impact millions
- 22:20Speaker 1: of users.
- 22:21Speaker 2: Yeah, well said. And as we wrap up part two
- 22:24Speaker 2: of our deep dive, I want to leave our listeners
- 22:27Speaker 2: with a question to ponder. Okay, if you had the
- 22:30Speaker 2: power to intercept and manipulate file system operations at such
- 22:35Speaker 2: a low level, what kind of creative solutions could you build?
- 22:38Speaker 2: What problems could you solve? Think about it, and we'll
- 22:41Speaker 2: explore some of those possibilities when we return for part three.
- 22:44Speaker 1: Back for the final part of our deep dive the
- 22:46Speaker 1: Windows kernel. Man, it's a wild ride down there, it is,
- 22:49Speaker 1: but we're not done yet. We're exploring these mini filters,
- 22:53Speaker 1: these tiny but mighty drivers that can really just get
- 22:56Speaker 1: in there and manipulate file system operations at such a
- 22:59Speaker 1: low level. And if you thought the stuff we talked
- 23:01Speaker 1: about last time was impressive, Hold onto your hats, yep,
- 23:04Speaker 1: because we're going to go even deeper. Okay, exploring some
- 23:07Speaker 1: of the more advanced things these minifilter developers can do.
- 23:11Speaker 1: You left us last time with that challenge to really
- 23:13Speaker 1: imagine the solutions we could build if we had that
- 23:16Speaker 1: kind of power at our fingertips. Right, my mind is
- 23:18Speaker 1: still kind of boggled by the possibilities, to be honest. Yeah,
- 23:22Speaker 1: give me some inspiration. What are some of the really
- 23:24Speaker 1: advanced things that many filters can do.
- 23:27Speaker 2: So one area that Yosvovich really dives into is is
- 23:31Speaker 2: the world of file system transactions. Okay, remember how we
- 23:34Speaker 2: talked about IRPs being like these envelopes carrying information about
- 23:40Speaker 2: file system requests. Right, A transaction is kind of like
- 23:44Speaker 2: bundling several of those envelopes together and treating them as
- 23:47Speaker 2: a single, indivisible unit of work.
- 23:50Speaker 1: So it's like saying, either all these operations succeed or
- 23:53Speaker 1: none of them do exactly.
- 23:55Speaker 2: Yeah, like an all or nothing approach to file system changes. Yeah,
- 23:59Speaker 2: and the beauty of it is mini filters can actually
- 24:02Speaker 2: participate in these transactions. Oh wow, Right, so they can
- 24:05Speaker 2: register callback functions that are invoked at different stages of
- 24:08Speaker 2: the transaction, giving them the opportunity to like monitor, modify,
- 24:13Speaker 2: or even veto operations that are part of that transaction.
- 24:16Speaker 1: Hold on, mini filters can veto operations within a transaction.
- 24:20Speaker 1: That's incredible. Yeah, what would be an example of when
- 24:23Speaker 1: that might be necessary?
- 24:25Speaker 2: Okay, Let's say you have a mini filter that's responsible
- 24:27Speaker 2: for enforcing disc quotas, right, okay, So a user might
- 24:31Speaker 2: try to write a large file that would exceed their
- 24:34Speaker 2: allocated space right now within a transaction, this might involve
- 24:38Speaker 2: several smaller right operations, right okay, So the mini filter
- 24:41Speaker 2: can can monitor those operations, and if at any point
- 24:44Speaker 2: the quota is exceeded, it can actually veto the entire transaction,
- 24:49Speaker 2: preventing any of the rights from actually happening.
- 24:51Speaker 1: So the mini filter can act as kind of like
- 24:53Speaker 1: a guardian, right, ensuring that the integrity of the file
- 24:56Speaker 1: system is maintained even when you have these complex multi
- 25:00Speaker 1: step operations.
- 25:01Speaker 2: Exactly, and yatif of A highlights some fascinating use cases
- 25:05Speaker 2: for mini filters and transactions. He even talks about scenarios
- 25:09Speaker 2: where minifilters can initiate their own transactions, giving them even
- 25:13Speaker 2: finer grained control over how file system operations are performed.
- 25:17Speaker 1: It's amazing how much power and flexibility these mini filters offer. Yeah,
- 25:23Speaker 1: I'm realizing that mini filter development is not for the
- 25:25Speaker 1: faint of heart. You need a deep understanding of the kernel,
- 25:29Speaker 1: the file system, just a whole bunch of advanced concepts.
- 25:33Speaker 1: What are some of the other challenges that these developers
- 25:35Speaker 1: face and how do they overcome them?
- 25:37Speaker 2: Well, one challenge we touched on earlier is concurrency.
- 25:40Speaker 1: Right.
- 25:40Speaker 2: Remember we talked about how multiple threads might be accessing
- 25:43Speaker 2: the same resources concurrently, Right, and.
- 25:45Speaker 1: Those race conditions where the outcome of an operation can
- 25:48Speaker 1: depend on the unpredictable timing of different threads.
- 25:53Speaker 2: Yeah, exactly, And if you're not careful, those race conditions
- 25:57Speaker 2: can lead to data corruption, dem in stability, all sorts
- 26:01Speaker 2: of nasty surprises.
- 26:02Speaker 1: Yeah.
- 26:03Speaker 2: So mini filtered developers need to be very, very diligent
- 26:06Speaker 2: about synchronization, you know, using techniques like locks, semaphores, atomic
- 26:13Speaker 2: operations to ensure that shared resources are accessed in a
- 26:17Speaker 2: safe and orderly manner.
- 26:18Speaker 1: It's like choreographing a very complex dance, right, making sure
- 26:21Speaker 1: all the dancers are moving in sync. No one's stepping
- 26:24Speaker 1: on anyone else's toes.
- 26:25Speaker 2: That's a great analogy. And it's not just about synchronizing
- 26:29Speaker 2: access to data structures within the mini filter itself, right,
- 26:32Speaker 2: minifilters also have to synchronize their actions with other kernel components,
- 26:36Speaker 2: you know, like the file system, the ile manager.
- 26:38Speaker 1: It sounds incredibly intricate. Yusifovich mentioned something called opportunistic locks
- 26:43Speaker 1: or oplocks for short. Are those related to this whole
- 26:46Speaker 1: synchronization challenge?
- 26:47Speaker 2: They are? Yeah, So op blocks or a mechanism that
- 26:50Speaker 2: allows mini filters to acquire exclusive access to a file,
- 26:54Speaker 2: you know, for a short period of time.
- 26:56Speaker 1: So it's like putting a do not disturb sign on
- 26:58Speaker 1: a file while the minifilter is working on it precisely.
- 27:01Speaker 2: And this can be incredibly useful for operations that require
- 27:05Speaker 2: that exclusive access, you know, like caching, encryption, or deduplication.
- 27:09Speaker 1: But those operations might take a while to complete, right,
- 27:12Speaker 1: So what happens if another process wants to access that
- 27:14Speaker 1: file while the mini filter is holding that OP block.
- 27:18Speaker 2: That's where the opportunistic part comes in. Okay, So when
- 27:21Speaker 2: a mini filter acquires an op block, it can actually
- 27:24Speaker 2: specify the conditions under which it's willing to release that lock.
- 27:28Speaker 2: For example, it might release the lock if another process
- 27:31Speaker 2: wants to write to the file, but hold onto it
- 27:33Speaker 2: if the other process only wants to read from the file.
- 27:36Speaker 1: Okay, So it's a way for mini filters to kind
- 27:38Speaker 1: of share access to files in a controlled and efficient
- 27:42Speaker 1: way without sacrificing data integrity or performance exactly.
- 27:46Speaker 2: And Yosefovich goes into a lot of detail about the
- 27:48Speaker 2: different types of oplocks, you know, how they're acquired and released,
- 27:52Speaker 2: and the various scenarios where they can be used.
- 27:54Speaker 1: It's clear that oplocks are a powerful tool in the
- 27:57Speaker 1: minifilter developer's arsenal. But let's shift gears for a moment
- 28:02Speaker 1: and talk about performance. We've discussed how mini filters can
- 28:05Speaker 1: intercept and manipulate IRPs, and that could potentially introduce, you know,
- 28:10Speaker 1: additional processing overhead. How do developers make sure that their
- 28:14Speaker 1: minifilters are as efficient as possible and don't negatively impact
- 28:18Speaker 1: the overall performance of the system.
- 28:21Speaker 2: Performance is always a critical concern in kernel development, right,
- 28:26Speaker 2: and many filters are no exception. Yosefovich actually dedicates a
- 28:31Speaker 2: whole section of his book to optimization techniques okay, and
- 28:35Speaker 2: one of the key principles is to minimize the amount
- 28:37Speaker 2: of time that the mini filter spends processing each IRP.
- 28:41Speaker 1: Makes sense, so the less time the minifilter is holding
- 28:43Speaker 1: onto an IRP, the faster that request can be completed.
- 28:47Speaker 2: Exactly, And there are several strategies that developers can use
- 28:51Speaker 2: to achieve that. One common technique is to use caching.
- 28:54Speaker 2: You know, if a mini filter needs to perform some
- 28:56Speaker 2: complex operation like encryption or decryption, able to cash the
- 29:01Speaker 2: results of that operation so it doesn't have to repeat
- 29:04Speaker 2: the work every time the same request comes through.
- 29:06Speaker 1: So it's like keeping a cheat sheet handy, right, avoid
- 29:09Speaker 1: having to redo the same calculations.
- 29:11Speaker 2: Over and over again exactly. Another technique is to defer processing.
- 29:16Speaker 2: You know, if a mini filter doesn't need to act
- 29:18Speaker 2: on an IRP immediately, it can simply pass it along
- 29:22Speaker 2: to the next driver in the chain and then handle
- 29:24Speaker 2: it insynchronously later on.
- 29:26Speaker 1: So it's kind of like delegating a task to someone else, right,
- 29:28Speaker 1: so you can focus on more urgent matters precisely.
- 29:31Speaker 2: And there are even more advanced techniques like using work
- 29:34Speaker 2: items or system threads to offload processing to a separate context,
- 29:39Speaker 2: freeing up that main driver thread to handle other requests.
- 29:42Speaker 1: Many filter performance optimization. That sounds like a delicate balancing act.
- 29:46Speaker 1: That is a yeah, requiring and deep understanding of the
- 29:48Speaker 1: kernel's inner workings and a willingness to you know, experiment
- 29:53Speaker 1: and fine tune.
- 29:54Speaker 2: Definitely, but the rewards are worth it. You know, a
- 29:57Speaker 2: well optimized minifilter and provide really valuable functionality without noticeably
- 30:02Speaker 2: impacting the system's performance. Right, and Yusifovich provides some excellent
- 30:07Speaker 2: guidance on how to achieve that delicate balance.
- 30:10Speaker 1: This has been an incredible journey, it has. We've explored
- 30:13Speaker 1: the fundamentals of the Windows kernel. We delved into the
- 30:15Speaker 1: intricacies of many filters, uncovered a world of complexity and
- 30:20Speaker 1: power that most people frankly never even know exists. Right, But,
- 30:25Speaker 1: as we wrap up, one final question for you, For
- 30:27Speaker 1: those listening who are feeling inspired to learn more, to
- 30:30Speaker 1: maybe even explore the world of kernel development themselves, what
- 30:34Speaker 1: advice would you give them?
- 30:35Speaker 2: First and foremost, I would say, embrace the challenge, right,
- 30:39Speaker 2: I mean, clernel development is not easy, but it is
- 30:41Speaker 2: incredibly rewarding. It's like learning a new language, a language
- 30:44Speaker 2: that allows you to communicate directly with with the heart
- 30:47Speaker 2: of the operating system.
- 30:48Speaker 1: It's like cracking a secret code, gaining access to this
- 30:52Speaker 1: hidden world of knowledge and.
- 30:53Speaker 2: Power exactly, and as you embark on that journey, be
- 30:56Speaker 2: sure to arm yourself with the right tools and resources.
- 31:01Speaker 2: Pavel Yusifovich's book is an excellent starting point, provides a
- 31:04Speaker 2: really comprehensive and insightful guide to the world of Windows
- 31:08Speaker 2: Kernel programming.
- 31:09Speaker 1: Couldn't agree more. It's been an invaluable resource for us
- 31:12Speaker 1: as we've gone through all the twists and turns of
- 31:14Speaker 1: this deep dive, and for our listeners. If you're intrigued
- 31:18Speaker 1: by what you've heard today, I highly encourage you to
- 31:21Speaker 1: check out Yusifovich's book. A deep dive into a world
- 31:25Speaker 1: that is both fascinating and challenging, a world where the
- 31:29Speaker 1: possibilities are really only limited by your imagination.
- 31:32Speaker 2: And who knows, maybe one day you'll be one of
- 31:35Speaker 2: the people creating the software that powers our digital lives,
- 31:40Speaker 2: shaving the future of technology in ways that you never
- 31:42Speaker 2: thought possible.
- 31:43Speaker 1: That's a perfect note to end on. Thank you for
- 31:45Speaker 1: joining us on this deep dive into the Windows kernel.
- 31:47Speaker 1: Until next time, keep exploring, keep learning, and keep pushing
- 31:51Speaker 1: the boundaries of what's possible.