Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: David Brown Newsgroups: comp.arch Subject: Re: The freedom that give us C++ and C Date: Thu, 28 Jan 2016 17:06:41 +0100 Organization: A noiseless patient Spider Lines: 131 Message-ID: References: <2016Jan16.191355@mips.complang.tuwien.ac.at> <2016Jan26.190638@mips.complang.tuwien.ac.at> <2016Jan27.145520@mips.complang.tuwien.ac.at> <2016Jan27.183750@mips.complang.tuwien.ac.at> <2016Jan28.161620@mips.complang.tuwien.ac.at> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Injection-Date: Thu, 28 Jan 2016 16:03:53 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="dc28719e2ffeeb5899be5f5057009353"; logging-data="3847"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19oW/FZza25TGGpipY8VuXs1TdD+DqS/08=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: <2016Jan28.161620@mips.complang.tuwien.ac.at> Cancel-Lock: sha1:N/4/sMWIbAz0xweG1otSPgqTLlE= Xref: csiph.com comp.arch:33230 On 28/01/16 16:16, Anton Ertl wrote: > David Brown writes: >> On 27/01/16 18:37, Anton Ertl wrote: >>> David Brown writes: >>>> On 27/01/16 14:55, Anton Ertl wrote: >>>>> Ivan Godard writes: >>>>>> On 1/26/2016 10:06 AM, Anton Ertl wrote: >>>>>>> object is referred to outside of it's lifetime: access the location >>>>>>> where the object was located; you may get various results, but that's >>>>>>> still a lot more defined than nasal demons. E.g., the recent ssh >>>>>>> vulnerability was made really problematic by such a nasal demon: The >>>>>>> programmer wanted to erase some memory, but the compiler decided that >>>>>>> it could "optimize" the erasing code away, because the lifetime of the >>>>>>> object ended afterwards. >>>>>> >>>>>> >>>>>> Isn't this a genuine "volatile" bug in the program, that a compiler >>>>>> change happened to expose? he language defines volatile for when a >>>>>> value can be accessed from outside the compiler-visible range. If the >>>>>> program wants to influence the out-of-range behavior then it should have >>>>>> declared (or casted the access to) the valiable as "volatile". >>>>> >>>>> If you think that the compiler would actually produce code for the >>>>> erasing code if the variable was declared "volatile", you are wrong: >>>>> (might only be readable to >>>>> non-subscribers on Thursday 28th). >>>> >>>> No, I am correct - but the "volatile" must be used in the correct way. >>>> That article/comment simply shows that if you use "volatile" in the >>>> incorrect way, it does not help. >>> >>> Yes, if you use "volatile" in the way you suggested, by declaring the >>> variable as "volatile", does not help. But, following your pattern of >>> blaming the programmer, you then turn around and declare that you have >>> to do it the "correct" way (of course without saying what the correct >>> and the incorrect ways are). >>> >> >> I didn't suggest details of how to fix the problem using volatile > > You did: 'it should have declared (or casted the access to) the > valiable as "volatile".' No, I gave a rough plan of a solution - not details. The rough plan is correct - use volatile in an appropriate manner (by declaring the data as volatile, or casting the accesses as volatile). I did not give details of exactly how to fix the code, because I did not have the code on hand. And certainly I would not suggest making the data volatile and then passing it directly to a function that takes a non-volatile pointer, because that would be undefined, unhelpful, and wrong. > >> To repeat - volatile /will/ solve the problem. But you must use >> volatile correctly. Making the variable volatile and then passing its >> address to a function that takes a non-volatile pointer merely >> introduces undefined behaviour (and compiler error messages). What you >> must do is make the writes to the variable volatile. That can be done >> by making the variable volatile and writing directly to it, or by making >> a volatile pointer to the variable and writing through that. This can >> be done directly in the code, or via another function - but clearly it >> cannot be done using a function that drops the volatile nature of the >> pointer. >> >> Rather than telling you the correct solution, let me point you to a >> reference: >> >> > > All right, please post the correct solution, so you won't try to claim > I misinterpreted you as you did above. Should be easy if the page > contains that code (I only see boxes containing nothing on that page; > I guess the boxes should contain code). Note that the code in > question is from 2010, so C11-specific solutions need not apply. > I am sorry you seem to have trouble using a web browser to view a webpage. For your convenience, here is the incorrect code from that site. It is not an extract from the ssh code - I don't have that on hand. But it is a simple equivalent case. // Incorrect code void getPassword(void) { char pwd[64]; if (GetPassword(pwd, sizeof(pwd))) { /* Checking of password, secure operations, etc. */ } memset(pwd, 0, sizeof(pwd)); } The site gives a number of alternative correct solutions using OS-specific functions, C11 functions, and a C99 version of the C11 memset_s solution. Let me give you a clear solution: // Correct code void getPassword(void) { char pwd[64]; if (GetPassword(pwd, sizeof(pwd))) { /* Checking of password, secure operations, etc. */ } volatile unsigned char *p = pwd; for (int i = 0; i < sizeof(pwd); i++) { *p++ = 0; } } Happy now? This is not rocket science, it is a basic understanding of how C works. >> The programmer(s) in the ssh code were to blame. > > As I said: You like blame the programmer! And the nice thing (for > compiler writers) about the nasal demon interpretation of C is that > you can always blame the programmer. > How many times do you have to hear it? Yes, the programmer is to blame. If you really insist on finding someone else to take the fall, then blame the manager who hired the programmer, or blame the project manager who failed to ensure there were good code reviews to spot the error. (Note that I am not saying the programmer was incompetent - he made a mistake, as we all do from time to time. But it was a mistake that should not have happened, or should have been spotted.)