- Podcasts
- CyberSecurity Summary
- Windows Kernel Programming
About this episodeThis Book excerpts Windows kernel programming, covering Windows internals and kernel development. It explains process and thread…
You can listen and download our episodes for free on more than 10 different platforms:
https://linktr.ee/cyber_security_summary
Get the Book now from Amazon:
https://www.amazon.com/Windows-Kernel-Programming-Pavel-Yosifovich/dp/1977593372?&linkCode=ll1&tag=cvthunderx-20&linkId=9b6d8365c69fcecb652867203969ac92&language=en_US&ref_=as_li_ss_tl
Discover our free courses in tech and cybersecurity, Start learning today:
https://linktr.ee/cybercode_academy
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
Windows Kernel Programming
All right, jumping right into this deep dive Windows kernel programming. Oh yeah, you handed me some excerpts from Pavel Yosefovic's book, and I gotta say, some of this stuff intense. Yeah, it's intense. It's a little over my head. Yeh, that's why you're here, thankfully. But I'm ready to learn, ready to uncover the secrets of the kernel some most people never even you know, think about.
Well, Yusefavich definitely doesn't shy away from the complexity of it. We're talking processes, threads, virtual memory system calls, object managers, handles, I mean, the whole nine yards, and even getting into like setting up a kernel development environment, and you know, the nuances of programming at the kernel level.
Okay, so before we get too far ahead of ourselves, let's rewind a bit. Yu Seefavitch spends a good chunk of the book talking about processes and threads, you know, the building blocks of everything that happens in the kernel. It's been a while since I've had to think about that stuff. Remind me, why are they so important?
Okay, that's a helpful way to think about it. So the kernel is constantly juggling all these virtual maps, making sure that each process has the illusion of its own private memory space. But what happens when a process needs to do something that requires like kernel privileges, you know, like access hardware, modify systems settings. That's where system calls.
And you see, Fitch gets into a really in depth detail about how this works at like the low level. He talks about system call numbers and the system service dispatch table and all sorts of other kind of intricate mechanisms. Is there anything in particular about system calls that stood out to you, you know, anything that surprised you or made you think differently about how they work?
You Know what really struck me is just the level of indirection involved. Yeah, you know what I mean. Like, it's not just a simple matter of a user mode function calling a kernel mode function directly, right. There are layers upon layers of abstraction, like checks and balances to ensure security and stability.
Well, centralizing resource management this way through the object manager has a lot of benefits, right. It enforces consistency, it prevents conflicts, it makes it much easier to track an audit resource usage, and plus it provides kind of this uniform interface for accessing and manipulating resources regardless of their type.
Why is that, Well, it's all about security and isolation, right. By giving each process its own handle table, the kernel can control exactly which processes have access to which objects, and it can also prevent one process from messing with another processes objects, even if they happen to have the same handle value.
Right, So it's like each process gets its own set of keys to access the kernel's resources, and those keys don't work for any other process exactly. But Yusufhovich also points out something kind of interesting. He says that the name that's displayed for a handle can have different meanings depending on the object type. What's that all about.
It can be a little confusing at first, but it actually makes a lot of sense when you think about it. You know, for some object types like mutex is, the handle name is simply the name of the objects. Yeah, But for other object types like files and directories, the handle name is actually the path to that object in the file system.
But all this talk about handles and objects in kernel mode is making me really curious about what it actually takes to write code that runs in this kind of privileged realm. Yusifovich has a whole section on setting up a kernel development environment, and let's just say it looks a lot different from my usual user mode coding setup. Oh yeah, it's a whole different world down there, it is.
Well, in user mode, if your code crashes, it usually only affects the current process, right, the rest of the system kind of keeps humming along. But in kernel mode, your code's running in the same address space as the operating system itself, So a bug in your driver can actually corrupt critical data structures, you know, leading to system instability or even a complete crash.
Yeah, I'm sarrying to understand why kernel developers are often seen as like wizards or gurus. But let's dive into some of the specifics of kernel programming. Here. Yasifavich talks about different memory pools that drivers can use, paged and non paged. What's the difference between these pools and why does it matter?
Okay, so think about it this way. Page pool memory is kind of like this, you know, flexible workspace that can be moved around as needed. Okay, right, So it could be swapped out to disk if the system is running low on physical memory and brought back in when it's needed again. But non paged pool memory, on the other hand, that's like your you know, dedicated office space. It's always available.
You're right, You're absolutely right, and that's why drivers have to be really, really careful about how they use non paged memory. It's a precious resource that should only be used for you know, code or data that absolutely cannot be paged out, things like interrupt handlers or data structures that need to be accessed very quickly and reliably.
Well, it's packed with important details. The driver object includes things like the driver's name, the device object that it's associated with, and probably most importantly, a list of function pointers called the major function array. Okay, and this array tells the operating system what IO requests the driver can handle.
That's exactly right. Yeah. Each entry in that major function array points to a specific function within the driver that's responsible for handling a particular type of IO request. So, for example, if the operating system needs to create a new file, It'll check the driver's major function array for an entry that corresponds to file creation.
Yeah, I remember him describing IRPs as like the language of the kernel, the way that all the different components communicate with each other. Right, But if I'm being honest, the whole concept of IRP still feels a bit abstract to me. Can you break it down for me? What do these IRPs actually look like and how do they flow through the system?
Precisely, and these IRPs, they get passed around from one component to another as the request is processed. So, for example, if a user mode application wants to write to a file, it'll make a system call, which will eventually result in an IRP being created in the kernel, and that IRP might be passed through several layers of drivers, each one potentially examining or modifying the request before it finally reaches the filesystem driver that's responsible for actually writing the data to disk.
So it's kind of like a relay race, with the IRP being passed from one runner to the next until it reaches the finish nine exactly. But with all these different components potentially handling the IRP, how does the system make sure that everything happens in the right order and that nothing gets lost or corrupted along the way.
Well that's where the io manager comes in, okay, right, It's like the racecord making sure all the runners are in their lanes and the baton gets passed smoothly. So the iron manager keeps track of all the IRPs in the system, it routes them to the appropriate drivers. Yeah, and it manages things like buffering and completion routines.
That's a great way to think about it. And that ability to intercept and manipulate those IRPs is really what makes minifilters so incredible powerful. They can, you know, they can filter, they can modify, redirect, they can even block IOA requests, all without the user even knowing it's happening.
Right. We talked earlier about antivirus programs using minifilters to scan files before they're written to the disc, but Yosefavic mentions a whole range of other applications like back up and restore data encryption, even performance optimization. It seems like there's a lot you can do with these things.
So imagine you want to restrict access to certain files based on the user's identity, you know, or group membership. A mini filter can actually examine the IERP for every file access request, check the user's credentials, and if the user doesn't have the necessary permissions, the minifilter can simply block the requests.
So this altitude allocation process is all about ensuring that mani filters can coexist peacefully and that the system remains stable and predictable. But let's dive a bit deeper into the actual structure of a minifilter. How are they implemented and what are the key components that developers need to understand? Okay, from what I gathered from Yosifovic, a minifilter is essentially a driver that registers a set of callback functions with the filter manager.
Okay, so imagine you're standing at a checkpoint on a road, right right, A pre operation callback is like checking the driver's license and registration before the car is allowed to pass through, and then a post operation callback is like checking the trunk after the cars pass through, just to make sure everything's in order.
Those results exactly. And those callbacks give mini filters just this incredible amount of flexibility, right. They can use pre operation callbacks to block requests, to modify data, to even redirect the IRP to a different device or file. And they can use post operation callbacks to log events, to clean up resources, or even to initiate new io requests.
It's like having checkpoints at every stage of the journey, allowing the mini filter to kind of control the flow of traffic and make sure that everything is running smoothly, that's right. But Yasifovich also mentioned something called contexts in the context of mini filters. What are those?
Okay? So, contexts are kind of like little sticky notes that a minifilter can attach to to various objects in the kernel, you know, yeah, like files, streams, or even volumes. They're a way for the mini filter to store information that's specific to that object, you know, like flags or counters or even cash data.
It sounds like a clever way to kind of keep track of things right and avoid having to store state information globally, which could potentially lead to you know, conflicts or race conditions exactly. But let's talk about something that's that's always on my mind when it comes to kernel development, error handling. Yusuphovic stresses the importance of failing fast and failing safely in kernel mode. Yes, why is that so crucial?
It sounds like AerR handling is serious business. In kernel development, it is. But let's move on to another kind of challenging aspect here, debugging. We talked earlier about how kernel debugging requires these specialized tools and techniques, right, but is there anything specific to mini filter debugging that developers should be aware of.
In a way. Yeah, But fortunately there are tools that can help Yasifovich mentions a special debugger expression called fltkd dot dll, which provides commands that are specifically designed for debugging minifilters, so you can use it to display information about loaded minifilters, their instances, attached volumes, even their internal state.
Yeah, well said. And as we wrap up part two of our deep dive, I want to leave our listeners with a question to ponder. Okay, if you had the power to intercept and manipulate file system operations at such a low level, what kind of creative solutions could you build? What problems could you solve? Think about it, and we'll explore some of those possibilities when we return for part three.
Back for the final part of our deep dive the Windows kernel. Man, it's a wild ride down there, it is, but we're not done yet. We're exploring these mini filters, these tiny but mighty drivers that can really just get in there and manipulate file system operations at such a low level. And if you thought the stuff we talked about last time was impressive, Hold onto your hats, yep, because we're going to go even deeper. Okay, exploring some of the more advanced things these minifilter developers can do. You left us last time with that challenge to really imagine the solutions we could build if we had that kind of power at our fingertips. Right, my mind is still kind of boggled by the possibilities, to be honest. Yeah, give me some inspiration. What are some of the really advanced things that many filters can do.
So one area that Yosvovich really dives into is is the world of file system transactions. Okay, remember how we talked about IRPs being like these envelopes carrying information about file system requests. Right, A transaction is kind of like bundling several of those envelopes together and treating them as a single, indivisible unit of work.
Yeah, like an all or nothing approach to file system changes. Yeah, and the beauty of it is mini filters can actually participate in these transactions. Oh wow, Right, so they can register callback functions that are invoked at different stages of the transaction, giving them the opportunity to like monitor, modify, or even veto operations that are part of that transaction.
Okay, Let's say you have a mini filter that's responsible for enforcing disc quotas, right, okay, So a user might try to write a large file that would exceed their allocated space right now within a transaction, this might involve several smaller right operations, right okay, So the mini filter can can monitor those operations, and if at any point the quota is exceeded, it can actually veto the entire transaction, preventing any of the rights from actually happening.
It's amazing how much power and flexibility these mini filters offer. Yeah, I'm realizing that mini filter development is not for the faint of heart. You need a deep understanding of the kernel, the file system, just a whole bunch of advanced concepts. What are some of the other challenges that these developers face and how do they overcome them?
That's where the opportunistic part comes in. Okay, So when a mini filter acquires an op block, it can actually specify the conditions under which it's willing to release that lock. For example, it might release the lock if another process wants to write to the file, but hold onto it if the other process only wants to read from the file.
It's clear that oplocks are a powerful tool in the minifilter developer's arsenal. But let's shift gears for a moment and talk about performance. We've discussed how mini filters can intercept and manipulate IRPs, and that could potentially introduce, you know, additional processing overhead. How do developers make sure that their minifilters are as efficient as possible and don't negatively impact the overall performance of the system.
Performance is always a critical concern in kernel development, right, and many filters are no exception. Yosefovich actually dedicates a whole section of his book to optimization techniques okay, and one of the key principles is to minimize the amount of time that the mini filter spends processing each IRP.
Exactly, And there are several strategies that developers can use to achieve that. One common technique is to use caching. You know, if a mini filter needs to perform some complex operation like encryption or decryption, able to cash the results of that operation so it doesn't have to repeat the work every time the same request comes through.
This has been an incredible journey, it has. We've explored the fundamentals of the Windows kernel. We delved into the intricacies of many filters, uncovered a world of complexity and power that most people frankly never even know exists. Right, But, as we wrap up, one final question for you, For those listening who are feeling inspired to learn more, to maybe even explore the world of kernel development themselves, what advice would you give them?
Couldn't agree more. It's been an invaluable resource for us as we've gone through all the twists and turns of this deep dive, and for our listeners. If you're intrigued by what you've heard today, I highly encourage you to check out Yusifovich's book. A deep dive into a world that is both fascinating and challenging, a world where the possibilities are really only limited by your imagination.
