The trickiest bug I've encountered
mokafive.tumblr.com
mokafive.tumblr.com
Then it blew up in production. Like hard crashes shortly after starting. Upon investigation, I found that entire pages of mapped memory were being overwritten with nulls, more or less randomly (4096 bytes at a time).
Turned out the bug was in mmap() due to the order patches were applied on various servers. The dev/qa servers were patched at different times than the production servers. That, that was hairy. And for a junior programmer to have to explain this to the tech leads and IBM support - I don't even know how many times I heard variants on "What's wrong with your code, really? mmap() isn't buggy!"
Ah, the 1990s. No Stack Overflow. No ssl (not even http). We did network programming by wrapping raw sockets in C and writing stream parsers in lex and yacc. Kids these days, they don't know hacking!
I was a senior developer but it was my first commerical job--I'd been in academia previously. I ran the change by the the most senior technical people in the company, all the way up to the guy who'd written the original application as employee #1. He OK'd it because, as he said, "strlen returns as short on the Mac anyway, and since our code runs on Mac as well as Windows it's a limitation we have to respect anyways."
A few years later the company stopped supporting the Mac.
A few years after that (and well after I'd left the company) one user site started getting crashes. I heard later that it took four senior devs a week to track down the cause, and much head-scratching because strlen was documented to return an int. They eventually found my #define, along with a comment that this eliminated so-many hundred warnings in the build, and that the change had been approved by the most senior people (I wasn't totally naive).
It turned out the problem at the specific site having the issue was users putting the entire contents of files into what amounted to tool-tips. It was totally unexpected user behaviour, but they'd found a place they could cache some useful data and we let them do it, so it should have worked.
Today I'd write a script that auto-edited all the cases where the problem occured, and regression test the hell out of it, but yeah: the '90's were a different time!
Raw _TCP_ sockets probably. Streaming over raw RAW sockets - now, that'd be pretty damn hardcore indeed :)
Still, it's weird to think that 20 years ago, just having TCP was kind of a luxury. I don't ever want to have to manage socket timeouts again.
How about this one from just last year: I'm writing an application that encrypts packets using datagram TLS with OpenSSL. Take a raw packet, send it through OpenSSL, it comes out the same on the other end and everything looks fine.
Then I try load testing it and start getting mangled packets. Application data where there should be IP headers and vice versa. So get out the debugger and find out that the data going into SSL_write() on one end isn't the same as the data coming out of SSL_read() on the other end. No TLS error, just mangled data.
So I install the OpenSSL symbols and discover that the data going into the cipher is exactly the same as the data that gets decrypted on the other side, hence no HMAC verification failure. But OpenSSL by default compresses data before encrypting it, and decompression output is not the data that was originally compressed.
TLS requires records to be delivered reliably. Datagram TLS, by contrast, is like UDP. Packets can be lost. And if they are, you can't use stateful compression or the lost data creates a hole in the decompressor's stream and corrupts the output. But OpenSSL was doing exactly that. So disable compression and the problem disappears instantly. (After three days in a debugger.)
That's what it means to write business-class software. Nobody worth having as a customer is going to build their business on your platform if your attitude whenever something goes wrong is "you shouldn't have been doing that in the first place".
(I am surprised to hear this story though. I actually found a bug in the NTFS buffer cache a few years ago which was introduced in (as I recall) Windows Server 2012. Maybe the Server organization are way more on the ball than the consumer OS organization, which is definitely possible. But they took it seriously and fixed it in a patch.)
Unfortunately, it's tricky to tell ahead of time which features are bad ideas and which aren't.
That answer exposes the deeper answer, too: "My job as a developer is to understand user needs so that our software can help them fulfill them, so if I don't understand why someone needs a feature I should dig in further before implementing it so I don't implement the wrong thing or the right thing in the wrong way." Not always possible given shedule constraints, though.
Admitedly, those rules don't seem to apply in this case, since if the OS allows you to do something that corrupts data, that's a problem no matter why the user wants to do it. If it can corrupt data, the OS shouldn't allow you to do it, end of story.
I will amend your statement to say that the OS itself should never corrupt your data, which I agree with entirely.
The post was not super clear about exactly what was happening (or it may be that my limited knowledge of Windows storage internals is keeping me from understanding it), but it sounds like the NTFS client requested a cache flush and then was issuing writes during the flush. I don't know what contract these operations have, but it may very well be the case that the user was violating the contract. If Microsoft responded with "don't do that", this may be the case.
But wait! Shouldn't Windows prevent the data from being corrupted? Or shouldn't NTFS fail the writes in this case? Possibly. And most likely inserting the checks to make this happen would increase write latency for every NTFS client, even the ones that don't behave in this way.
This reminds me of another scenario I encountered, with Veritas VxFS running on top of AIX. The user initiated a space reclamation, which was sending what you can think of as a delete to the storage array. And the user was also writing data to the device at the same time. Due to a race condition (which I can describe for you if you really care), the legitimate user data would sometimes be deleted.
Should VxFS have protected users against this case? Yes. Was VxFS violating the SCSI protocol? No. Was the storage array violating the SCSI protocol? No. (Has VxFS fixed this bug, almost three years after I discovered it? No comment.)
It's always a lot more complicated than it seems on the outside.
Calling a write while flushing the same data from another process (as the OP reported) or thread is perfectly legitimate set of operations. If these operations are not supposed to be running simultaneously, that has to be enforced by the OS kernel. The "right" way would be for the OS to serialize these operations internally. But even returning an error for the write might be an acceptable (though not nice) way to handle it. What's absolutely not acceptable is randomly corrupting data.
All I'm saying is that no real, sane OS is going to be capable of protecting itself against every possible misuse. Remember that these are developers coding to an API we're talking about, not end users.
What if you have an API that takes an out pointer. I pass in data that I own, then I free that pointer in another thread. If I'm in userspace, I can blow up with SIGSEGV. If I'm in the kernel, maybe now you've just scribbled all over somebody else's memory. Shame on you for corrupting data.
APIs can always be abused. All the API developer can do is try to protect against obvious forms of abuse. I guarantee you every operating system that supports simultaneous multiprocess execution has some series of APIs that, when called in parallel, will corrupt your data.
Only for a wide interpretation of 'misuse'. In the case you described, the kernel is still doing what it was asked. In the article, it wasn't. Situations where one command invalidates another should be very uncommon and very documented.
A real, sane OS should be resilient to any syscalls in any order without triggering internal bugs.
So yes, an OS should allow you to call its API without triggering internal bugs. But that's kind of a straw man.
There's an interesting line that you're exploring, though, and I would like to dig deeper. You said "the kernel is still doing what it was asked". Let's ignore the bug described by the article for a moment and look into this.
Let's say I have two threads. Each one does a write to the same LBA range. You wind up with a file that doesn't represent the full contents of either write (say it's a 8192-byte write, and your file has 4096 bytes from the first write and 4096 bytes from the second write). Do you consider that to be a bug in the OS or a bug in the application client?
(It's obvious that two conflicting writes will result in someone losing their data. The part of the scenario I'm exploring is that in this case everyone lost their data.)
In the absence of documentation saying otherwise, I would expect the calls to be serialized, and one to happen entirely before the other.
Now if they each did two writes at about the same time, it's no longer the OS's problem if the threads interlace badly.
That's not a straw man. That's the exact topic under discussion in this thread.
"Let's ignore the bug described by the article for a moment and look into this."
And that my friend is the definition of a straw man :-)
If the topic you're discussing is that the clients of an OS API should not encounter a bug...uh, I mean, I don't think you'll find anyone to disagree with you. It's not like Microsoft engineers put that bug in there thinking it would be OK. They even acknowledged that it was a bug...it just wasn't high priority because the caller (presumably; again, I know nothing about these syscalls) is not following best practices. Someone's pet bug got deprioritized. This is not news.
The topic I thought we were discussing was whether it's the OS's responsibility to prevent callers from misusing their APIs in a way that causes stupid things to happen, even when the caller is doing something stupid.
I think the latter is a more interesting conversation, but I apologize if I interrupted your discussion of the former.
So it would seem.
"It's not like Microsoft engineers put that bug in there thinking it would be OK."
You say that now. But the reason my strident response was triggered was because of what you said in your original post that I replied to:
"I don't know what contract these operations have, but it may very well be the case that the user was violating the contract. If Microsoft responded with "don't do that", this may be the case."
That is quite absurd on it's face. I replied with how the contract was nothing more or less than the syscall API and there was no margin for negotiation on kernel's part when it comes to corrupting data when a user program calls those APIs in whatever order it pleases.
Subsequently, your arguments seem to have become more elaborate with a lot more caveats added. I think it has stopped being fruitful for me to respond at this point.
It was corrupting it with data from other files on the disk, which could have been sensitive.
Where this was particularly troubling was that an unprivileged user could then use this as an exploit/attack on the system to get it to leak pages of system files. This is where fun stuff like pass-the-hash begins.
(Going back and reading the post again, it seems that it's even worse than that, since a virtual environment hitting this bug could get data from the host. This opens an attack vector for a guest to bypass the hypervisor and compromise sensitive host data. I don't know if data from other VMs on the system could also be exposed in this way, but that would also be quite bad.)
Anyway, I think my rambling caused my point to get lost, because I wasn't trying to argue that Microsoft are justified in their "don't do that" comment. But no sane, realistic OS can prevent against every hare-brained thing a driver developer is going to do.
My favorite set of APIs is AWS. You know why? Because they've realized they hold two very weighty sticks that they can use when designing, and they've put them in place all over.
1. They can make any arbitrary message to the API cost the user money every time they send it, to disincentivize using that part of the API thoughtlessly. That's whether or not they expect this to be an actual revenue stream at the rates people are charged for reasonable usage.
2. They can put a "soft cap" on any arbitrary resource, so that you have to phone them and get the cap raised if you want more than [some reasonable number] of something. This likewise disincentivizes bad designs that use a nigh-infinite number of costly somethings to accomplish tasks that could be just as easily accomplished some more idiomatic, less costly way.
AWS doesn't prevent you from doing stupid things... but it makes you really not want to. I love it.
Their docs make it easy to make the mistake of thinking that fine-grained controls are available for most things, but when it comes to really important things like being able to segregate a production and Dev VPC, their APIs basically force you to grant permissions to everything or nothing.
Some examples of things I've hit: Not being able to restrict a user to only change a specific routing table Not being able to restrict a user to only change a specific elastic NIC
I'm consistently surprised at what's missing from their API and couldn't disagree more about being happy with it.
I wouldn't say I'm happy about it, but neither am I unhappy, and neither am I happy about anything in the world of security (also in today's task list is updating https cipher lists... again...). Not even the simplest thing in security is easy. For example, the basic concept of a password is simple, but actually implementing it? Ugh - it involves every layer from backend to frontend to user training (the hardest part - no sticky notes, no friendly phone calls, no passing around in emails...).
Anyway, for those not used to IAM 'Conditions', an example of use. The following allows Packer (an AMI builder) to destroy any EC2 instance, but only if they have the tag 'name' as 'Packer Builder'. Conditions don't work for everything, so they're not a workaround to get fine-grain everywhere, but they do add a lot of flexibility.
"Sid": "AllowInstanceActions",
"Effect": "Allow",
"Action": [
"ec2:StopInstances",
"ec2:TerminateInstances",
"ec2:AttachVolume",
"ec2:DetachVolume",
"ec2:DeleteVolume"
],
"Resource": [
"arn:aws:ec2:us-east-1:xxxxxx:instance/*",
"arn:aws:ec2:us-east-1:xxxxxx:volume/*",
"arn:aws:ec2:us-east-1:xxxxxx:security-group/*"
],
"Condition": {
"StringEquals": {
"ec2:ResourceTag/Name": "Packer Builder"
}
}I do a lot of my AWS work in CloudFormation. When I hit a wall, the answer is pretty much always to stand up an EC2 instance that can speak SNS, grant it larger-than-necessary permissions to my VPC, teach CloudFormation about it as a custom resource type, and have it serve as a proxy for the not-configurable-enough resource, allowing it to assert its own policy and make third-party calls before making the real callback into your VPC [or not.] It's the AWS equivalent of writing a factory method to wrap a badly-written constructor.
To generalize that thought: IAM "users" are made to either be people (e.g. your developers, your ops people), or representative tokens for entire third-party organizations (e.g. a CI bot.) Despite the existence of IAM roles, IAM isn't really made to assert "machine-agent"-granular permissions.
Instead, what you really want is to imagine a third-party service running in the AWS cloud that does exactly what you want. You would grant that third-party's IAM user overly-wide permission to play with your VPC, but trust it to only do what it should, because, obviously, you have a business relationship and it would be dumb of them to abuse it.
As soon as you can see what API needs to exist, you can turn around and become that very same imaginary third-party: make a separate AWS account, stand up an API server in it that takes requests to do what your "clients" want, and then, in turn, make requests to the AWS APIs on their behalf to accomplish those things.
AWS isn't a high-level framework; it's a kit of low-level tools. (This is really what the PaaS vs IaaS distinction implies, I think.) AWS is built assuming that you're willing and able to take their tools and pipe/script them together to build the higher-level components you need. And, since AWS is for web services, that assumption comes in the form of expecting you to be able to pipe, hook, or wrap any of their APIs to/with/in your own API.
Once you prove it, it's completely free. Ultimately memory dumps are the way to go when it comes to MSFT bug reports, if you can catch their bug red-handed and snap it to disk things go real smooth.
I always found it incredibly difficult to report anything to Microsoft and usually only get a 'we have reported it to engineers and it will be fixed some day' message.
It was the interaction between the Cache Manager and the Memory Manager in managing the rare transient state ModifiedNoWrite of the cache pages when dealing with reentry IO read requests. The cache page status became Modified when its content was filled in from disk, but it's marked as NoWrite to avoid being flushed out by the Memory Manager. The physical page backing the cache page can't be reused since it's dirty (Modified) but the Memory Manager can't flush it out (NoWrite). Slowly over time as more pages are read, the system would run out of physical pages.
The Cache Manager was supposed to change the cache page status back to Standby after the read returned from lower layer. But with reentry IO read requests, it won't do it when upper layer IO request buffer passed straight down. The work around was to allocate a separate buffer to interact with the Cache Manager and copied the content back to the upper layer IO request buffer, incurring an extra copy.
At the end I intimately knew about the Windows Cache Manager and Memory Manager more than I needed to know.
https://news.ycombinator.com/item?id=9576917
Edit: That must be why this post from 2013 (update the title!) discussing an issue fixed in 2011 hit the front page...
(Software is hard and often buggy, filesystems included. Check your backups! Even if your filesystem is perfect, I spilled water on my laptop yesterday, and you could, too.)
I don't think it's likely that I'll spill water on your laptop, though.
As the old saying goes, nobody cares about backups — people care only about restores.
If you don't test it, how do you know?
For example, one person told me he can't understand antivirus software and why people buy it, because he never got a virus. I asked him "how do you know you didn't get a virus?". He just looked at me, not saying a word. I hope my point got across though. If you aren't checking, you don't know. Same could be said about hacking these days. You secure your system, that's good, but if you don't have something to detect hackers, you are the same as the guy without antivirus and the guy without tested backups. You just have no idea whether or not you are protected.
"How do you know you didn't get a virus?"
I don't. But it's not epistemically clean to let other people set your priors for things like risk, if they have a financial interest in making you worry.
https://en.wikipedia.org/wiki/Lucas_critique
And sadly one reason why IT is a easy department for the beancounters to downsize.
To be specific, I wanted to know the specific conditions the article talks about under which this causes an issue. Flush will cause writes (obviously) so issuing writes to a file which is pending a flush is an interesting scenario.
Now we just need a copy of it.
When you wrote to a file (using WriteFile or fwrite()), it first extended the file length, and then committed the buffer. This is supposed to be atomic - that is, you should never be able to see the length already extended but the data not yet there. And it apparently was atomic if both reads and writes came from the local machine, or both came from the network - however, if the write was on the local machine but the read was from the network, locking was missing, and it WAS possible to read zeros instead of the real data (but only because of a race condition - reading the same file again later would give the expected answer)
Tried to get Microsoft to at least confirm this bug, to no avail - there was no one interested in talking to a lone freelance developer back then.
The flight of 4 jets were able to return to US mainland only because the accompanying tanker was able to guide them back.
Same here. I think it's a rare person that's been living and working with computers for more than 20 years that doesn't develop an innate distrust of them or at the very least a subtle set of supersticious about how they work, how they're supposed to work, and the best way to get your work done.
If you don't trust the OS, don't use it. If you use it, you're giving it access to all your files, full stop.
Remember, the bug "can cause arbitrary host OS pages to be written to the target file in some circumstances"
something like
do_encryption(origin, destination)
save_to_disk(destination)
You're clearing origin right before save_to_disk?Oh I'm so sorry, destination had only been allocated for you, writing to it caused some other pages to be evicted and other processes being scheduled and the bug to be triggered and your data is still there. You lost