‫Hello and welcome, everybody, in this lecture.
‫We are going to fix a small bug in the disk streamer that I identified the other week.
‫So unfortunately, you know, this is kernel development, small mistakes to happen.
‫However, the important thing is, is that we fix them.
‫So all I want to do is go to disk and stream adult, see, and we're going to scroll up here and we're
‫going to include
‫ascribable dot org.
‫That is scroll down to read.
‫If you're interested in increasing your skills in solving bugs, you can pause the video and take a
‫look at this code and try to figure it out yourself.
‫But the disk streamer read is wrong.
‫So post video if you want to figure it out yourself.
‫Otherwise, I will now explain the problem.
‫We only read in a sector here, which was what we wanted to happen, ok.
‫However, if the total to read plus the offset exceeds the Buffa, we don't take that into account.
‫It will actually read past the buffer.
‫So OK, the total can be bigger than the SEC to size, but that doesn't mean our offset can't be 400
‫or something.
‫Right.
‫And if our offsets 400 or something and we're reading 256 bytes, then we're going to overflow this
‫buffer.
‫So the best case scenario that corrupts the data you're reading, worst case scenario, it could be
‫exploited by a malicious program trying to hack the kernel.
‫OK, so that does need to be solved.
‫So we're going to fix that now.
‫So we're going to do now is we're going to go above the buffer here and we're just going to say sample
‫overflow
‫equals offset plus total to read.
‫Above all, equal PCOS insecticides and this little total sorry, we've done here to lead it and we're
‫going to redefine that here, so we're going to go in it told to read.
‫Equals total.
‫So overflow, offset plus told to read above were equal sector size.
‫So what we're now saying is, is if the offset is 400 something and we're reading 256 bytes, then obviously
‫that is above 200.
‫That is above 512 bytes.
‫So overflow be true.
‫And in this way, we know if if we post this operation, there will be an overflow.
‫So we're now aware of that.
‫So we're now going to handle that situation.
‫So just under the Buffa, we're going to say if overflow total to read minus equals offset plus total
‫to read minus sector size.
‫So what this will do, it will essentially make what we're reading Phet.
‫So for example, let's pretend our offset is 400.
‫We're going to get a python here, actually.
‫So we're going to start Python and let's just pretend that an offset is 400 total to read is 200.
‫OK, so we know that it exceeds the 512 by buffer.
‫OK, so what we now do is we say toters to read minus equals offset, plus the total to read minus five,
‫June 12 for the SEC to size if we now print total to read.
‫We see 112 now, if you do offset plus one hundred and twelve, you get 512, so you can see how that's
‫working, right.
‫We minus off the difference so that we can ensure that we don't overflow the buffer.
‫So then we're able to read from the offset to the end of the sector, but not further than that.
‫So what we can then do is we can read the rest of the data with recursion like we did down here on line
‫55.
‫So this code now needs to be changed.
‫So instead of reading the next sector or whatever was left available, we're going to take into account
‫what we were supposed to read versus what we actually did read.
‫OK, so this is fairly easy or no longer checking if the total is above the sector size.
‫We're checking if we overflowed.
‫So if overflow, I remember overflow is true.
‫If the offset combined with the total to read is above the sector size, that means we will overflow
‫the buffer if we process it OK, which is why we resolve that problem on line 36 here.
‫So yeah, if we if we did overflow, then we're just going to go total minus total to read, OK?
‫And that should fix our problem.
‫So just to demonstrate how that works, if we get a python again.
‫We know the original total was 200 before we changed anything.
‫OK, we know after we did this operation, we had 112 to read.
‫So if we do print 200 minus one one two, then we know that we have 88 bytes left to read at 88 to one,
‫one, two, and you get the original 200.
‫So this equation here, Total Minus told to read, gives us 88 more bytes to read.
‫Now, Outpoint is pointed correctly because it's been adjusted here.
‫Well, we went out plus.
‫Plus.
‫OK, so then we just call ourselves again.
‫So it's a bit of recursion going on.
‫Now, as I said, you do have to be careful with recursion that you don't overflow the stack because
‫it's quite easy to do.
‫But nevertheless, this is a fine solution for what we're doing here for this training exercise.
‫So, yes, what will then happen is when we do this remarried read again, it'll go back over here and
‫it will read the next sector.
‫OK, so that concludes this lecture.
‫We've now fixture this stream and you shouldn't run into any problems if we do make clean bill that
‫S.H..
‫We can see that Kompas absolutely fine.
‫Thanks for watching.