Skip to content

Don't trust the OPENSSL_ia32cap envvar more than the cpuid instruction - #5860

Closed
bernd-edlinger wants to merge 3 commits into
openssl:masterfrom
bernd-edlinger:dont_trust_ia32cap_envvar_more_than_cpuid
Closed

bernd-edlinger wants to merge 3 commits into
openssl:masterfrom
bernd-edlinger:dont_trust_ia32cap_envvar_more_than_cpuid

Conversation

@bernd-edlinger

Copy link
Copy Markdown
Member

Fixes test failures with illegal values in OPENSSL_ia32cap, i.e.:
OPENSSL_ia32cap=0xFFFFFFFFFFFFFFFF:0xFFFFFFFFFFFFFFFF make test

@dot-asm this one's for you ;)

@dot-asm

dot-asm commented Apr 4, 2018

Copy link
Copy Markdown
Contributor

Not that I would endorse following, but if you google for "OPENSSL_ia32cap VPS enforce AES-NI", you'll find people who make use of unmasked value. Not that they do it right, but they seem to appreciate it. One should also recognize that OPENSSL_ia32cap is kind of a troubleshooting thing, i.e. you are supposed to know what you are doing. And taking responsibility for SIGILL is kind of part of knowing what you're doing. Also imagine somebody submits problem report and says "I've assigned this value and get this error". You try to reproduce it and don't get the error. Is it because a bit was masked on reporter's system or yours? I mean if reported has the bit and it was silently masked on your system, you wouldn't know. With unmasked assign you'd get SIGILL and know directly that you can't reproduce the environment. In other words I for one see value in unmasked assign. But sure, I can as well recognize value of masked. So maybe if we choose to do something about it, I'd say that it would be appropriate to recognize more operators. I mean now it recognizes ~ for bit masking, so one can say that it should recognize = for unmasked assign, and even | for unmasked bit set. On related note one can wonder if minor release is right spot to do this kind of thing. I mean adding new operators is ok, question is rather about changing the behaviour. Well, it's troubleshooting thing, so it's not something that should be found in production environments, but we all know that there is nothing more permanent than temporary measures. Either way, doc/man3/OPENSSL_ia32cap.pod would need to be updated correspondingly.

@bernd-edlinger

bernd-edlinger commented Apr 4, 2018 •

Copy link
Copy Markdown
Member Author

I just stumbled over this because I looked for getenv which is used in setuid process.
without OPENSSL_issetugid guard. While other environment variables like OPENSSL_CONF
could easily make a privileged code execution kind-of-thing, especially when this policy will be
enforced on all applications in future, this OPENSSL_ia32cap environment value can still
cause suprising effects.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

So enabling something that is not supported by the cpuid is probably a nice thing,
but maybe we should play safe when we run in ring0 already?
What also comes in my mind, is maybe test the instruction first when the
environment variable advertises it?

@bernd-edlinger

Copy link
Copy Markdown
Member Author

I read the article you mentioned, but do you believe the hypervisor will preserve the
whatever-registers that this NI code uses when they do not set the right cpuid flags ?

@dot-asm

dot-asm commented Apr 4, 2018 •

Copy link
Copy Markdown
Contributor

I read the article you mentioned

Once again, I'm not endorsing what they do, rather contrary. I'm not even saying that they do the wrong thing right, even here rather contrary :-) What I'm saying is that I see value in unmasked flag assignment nevertheless. Or in other words I argue against suggested modification as it stands now and suggest to consider keeping unmasked assignment, for example by means of recognizing additional operators like = and |. This is provided that we agree that minor release is appropriate place for doing this.

As for issetugid. One can probably argue that it would be appropriate to mask value with cpuid in such case, yes. But it's it would be conditional masking, and hence arguably reasonable.

And whatever outcome would be, OPENSSL_ia32cap.pod would need adjustment.

@dot-asm

dot-asm commented Apr 4, 2018

Copy link
Copy Markdown
Contributor

So enabling something that is not supported by the cpuid is probably a nice thing, but maybe we should play safe when we run in ring0 already?

I don't follow, what ring0?

What also comes in my mind, is maybe test the instruction first when the environment variable advertises it?

You mean non-x86 style when you set SIGILL handler and see if instruction crashes? Rather not. I'd say it would be unnecessary complication (and implicit endorsement of doing wrong thing ;-)

@bernd-edlinger

Copy link
Copy Markdown
Member Author

I don't follow, what ring0?

I mean privileged, like issetguid at least.
It is also not about adding more functionality in the moment, it is about adding safety.
And IMO a compile-time flag or something that allows you to set apparently unsupported bits?

@bernd-edlinger

bernd-edlinger commented Apr 4, 2018 •

Copy link
Copy Markdown
Member Author

And whatever outcome would be, OPENSSL_ia32cap.pod would need adjustment.

Definitely, but also other OPENSSL_ppccap OPENSSL_armcap and OPENSSL_sparcv9cap
would need a place to be documented. Maybe at least add a section in the OPENSSL_ia32cap.pod ?

@bernd-edlinger bernd-edlinger changed the title Don't trust the OPENSSL_ia32cap envvar more than the cpuid instruction WIP: Don't trust the OPENSSL_ia32cap envvar more than the cpuid instruction Apr 4, 2018
Comment thread crypto/cryptlib.c Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless I am mistaken OPENSSL_ia32cap=0:~1 did not initialize OPENSSL_ia32cap_P[2..3]
and used an uninitialized value in the &= here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why I added the OPENSSL_ia32_cpuid call in ifdef-OPENSSL_OVERRIDE_CPUID above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0:~1 did not initialize OPENSSL_ia32cap_P[2..3]

Well, it you set first quadword, then second one is cleared. One of problems is that there are certain relationships between bits in first and second quadwords(*), and instead of tracking them, it's just easier to say that if first quadword is set, second one is simply cleared. Yes, you can sense contradiction, because if there are relationships, then masking bits in first quadword with ~ should track these relationships. Admittedly code is a bit heuristic and user should mostly be aware of what's going on. There are invalid/inappropriate combinations, but tracking them down is daunting task and it's not clear if it's worth the effort... But it might be just me...

(*) For example if you mask xmm, then you shouldn't be using ymm.

@dot-asm

dot-asm commented Apr 4, 2018

Copy link
Copy Markdown
Contributor

I'm sorry, but I [for one] am opposed to introduction of another knob. I can accept an argument in favour of conditional masking depending on issetugid (though I don't quite recognize importance yet), but not another knob, sorry.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Sorry, I know if's your baby...

But, I think masking certain bits, in the cpuid can not cause anything harmful.
And moreover it can be really useful to work around O/S bugs for instance.
But I still don't see what's the value in enabling CPU features that are not
advertised by the hardware.

@dot-asm

dot-asm commented Apr 4, 2018

Copy link
Copy Markdown
Contributor

Sorry, I know if's your baby...

That's not what I'm hinting at. Me being opposed to additional knob is because I'm opposed to any additional knob, it has nothing to do with the fact that suggested knob affects OPENSSL_ia32cap. Nor am I saying that one can't criticize it, you're welcomed to do so. But I'd be opposed to making it overly complex.

And moreover it can be really useful to work around O/S bugs for instance.

And you can do it as is, just stick with ~...

@bernd-edlinger
bernd-edlinger force-pushed the dont_trust_ia32cap_envvar_more_than_cpuid branch from 405b35a to 96f4640 Compare April 13, 2018 11:32
@bernd-edlinger bernd-edlinger changed the title WIP: Don't trust the OPENSSL_ia32cap envvar more than the cpuid instruction Don't trust the OPENSSL_ia32cap envvar more than the cpuid instruction Apr 18, 2018
Comment thread crypto/armcap.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outcome would be wrong if you clear PMULL and set AES. Because it will never probe for AES. However, instead of treating it as special case, it might be appropriate to simply switch to (mask & CAP) && sigsetjmp() in all cases. Not only will it fix this corner case, it would be more readable. I mean sigsetjmp is effectively part of the probe, so you first decide if you should perform probe, then perform it. As opposite to making half way into probe then deciding if you should proceed to final part of the probe.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the C90 standard very carefully, and it says setjmp must be called only in one of these ways:

   An invocation of the setjmp macro shall appear only in one of the
following contexts:

 * the entire controlling expression of a selection or iteration statement;

 * one operand of a relational or equality operator with the other
   operand an integral constant expression, with the resulting expression
   being the entire controlling expression of a selection or iteration
   statement;

 * the operand of a unary ! operator with the resulting expression
   being the entire controlling expression of a selection or iteration
   statement; or

 * the entire expression of an expression statement (possibly cast to void).

so the suggested syntax is non-compliant.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would if (mask & (ARMV8_PMULL|ARMV8_AES)) do the right thing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work:

if (mask & ...) 
   if (sigsetjmp(....) 

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, keyword is to flip conditions.

Comment thread crypto/armcap.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still opposed to the additional knob. Question is how many would actually use it, especially in suggested form. By this last remark I mean that it would make sense if it would also resolve run-time switches at compile time, and even remove unused code. But otherwise it would really be something for somebody to occasionally test something in non-production environment. But thing is that in such case that somebody can just as well simply temporarily modify the code by adding an assignment followed by return. [I did it at several occasions.] As for making it real in sense that capability override flag would resolve run-time switches and drop unused code. Trade-off here is if savings are worth the trouble of maintaining the #if spaghetti. I mean OpenSSL is not exactly suitable for resource-constrained environments and space savings would be negligible in proportion to the rest.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness sake I probably should mention that there were suggestions to take run-time switches out of assembly modules and break them into segments so that linker can omit unreferenced code. The question was risen rather in context of code reuse in other projects [more suitable for resource-constrained environments]. I'd say this would be more meaningful goal than to introduce #if spaghetti that would be OpenSSL-specific. However, such goal would have to be supported by multiple projects, there ought to be some synergy...

@dot-asm

dot-asm commented Apr 19, 2018

Copy link
Copy Markdown
Contributor

Let me rephrase a little bit. How is suggested modification for x86 essentially different from simply not accepting input without ~? I mean why not simply recognize only values that start with ~? I mean if it's about masking, then ~ does do exactly that...

Just in case, once again, it's not like I'm totally opposed to changes in this area, but question still is if it's right moment, and maybe while we at it, then it would be appropriate to go out of the box all the way. Say symbolic names like ~SSE...

@bernd-edlinger

Copy link
Copy Markdown
Member Author

First I wanted to be able to meet your request to be able to try out different bits
although not enabled by cpuid. While I think it is too unsafe to use an environment
variable for that, I don't see any problem with using a compile-option.

Together with the build_all_generated I do see a use in embedded O/S like
eCos. That is when I think of cross-compilation I know exactly which CPU
it will be.

I do for instance maintain an Cortex-A8 based eCos environment,
where today openssl runs with no-asm, but one day, it will be possible
to specify the CPU capability using a compile-time define.

Probing the CPU for NEON won't work, because the O/S port does not
save the NEON registers at the task switch, that was considered unnecessary)
And environment values are likely too clumsy or not implemented.

That's why I thought a compile option would be really what I will need some day.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Looks like we fail to build with --strict-warnings enabed:

crypto/armcap.o -c -o crypto/armcap.o crypto/armcap.c
In file included from crypto/armcap.c:18:
crypto/arm_arch.h:72:7: error: "__ASSEMBLER__" is not defined, evaluates to 0 [-Werror=undef]
 # if !__ASSEMBLER__
       ^~~~~~~~~~~~~
cc1: all warnings being treated as errors

Comment thread crypto/armcap.c Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

??? What's wrong with else here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if mask = PMULL | AES but runtime check fails the AES check will not be tried.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously the else was on the "if (sigsetjmp...)"
but now it is on the "if (mask & PMULL)"
either way I can miss the AES check.

@dot-asm

dot-asm commented Apr 19, 2018

Copy link
Copy Markdown
Contributor

Probing the CPU for NEON won't work, because the O/S port does not save the NEON registers at the task switch

My understanding is that if you don't flip a bit, then NEON instructions will fail. So that if OS doesn't "want" to support NEON, then it just doesn't do anything, bit is off at power-up, instructions will fail, and so probe will work. And of course if it flips the bit, but doesn't do anything at context switch, in which case one can wonder if it's right... Is it actually case? I.e. it flips the bit but doesn't handle context switch? I.e. NEON probe in fact succeeds on Cortex-A8?

Though I can understand the desire to avoid the probing even if probe fails.. I don't know much about eCos, are there user vs. kernel lands? If no and code is sufficiently privileged to read that coprocessor register...

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Yes, I agree, it that is how you say, then automatic probing will likely do the right thing :)

eCos is just a multi-threaded micro-kernel with everything linked statically.
No bash, no rcS, no fork(), no exec(), no user-kernel separation.
Nevertheless we have openssl, https and and and...

@bernd-edlinger

Copy link
Copy Markdown
Member Author

And no guard pages on the tasks!

@bernd-edlinger
bernd-edlinger force-pushed the dont_trust_ia32cap_envvar_more_than_cpuid branch from 55e0ea9 to 376a8e6 Compare April 22, 2018 07:42
@mattcaswell mattcaswell added this to the 1.1.1 milestone Apr 25, 2018
@richsalz

richsalz commented May 6, 2018

Copy link
Copy Markdown
Contributor

ping @dot-asm and @bernd-edlinger to come to an agreement here :)

@t8m

t8m commented Jun 22, 2018

Copy link
Copy Markdown
Member

I believe the only thing missing is to guard use of OPENSSL_ia32cap with the issetuidgid call. There is no point in making this mostly debugging interface fully user-proof.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Thanks, I was considering that as well, but what if the user set OPENSSL_ia32cap
because the default value is known to cause failure?
Then setuid apps would break because of ignoring the OPENSSL_ia32cap.

@t8m

t8m commented Jun 22, 2018

Copy link
Copy Markdown
Member

You mean that a setuid app would depend on the caller to set OPENSSL_ia32cap properly otherwise it would not work? I cannot imagine such broken system or said in other way, such broken system deserves its breakage anyway.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

But if that is the case, why do we have this kludge at all?

@t8m

t8m commented Jun 22, 2018

Copy link
Copy Markdown
Member

For bug testing, development, performance testing?

@dot-asm

dot-asm commented Jun 22, 2018

Copy link
Copy Markdown
Contributor

I think it would be easy to make the inclusion of NEON code dependent on OPENSSL_*CAP_OVERRIDE either with this PR or a follow-up.

And what I'm saying is that non-NEON option would be all one needs. There is really no point to have OPENSSL_ARMCAP_OVERRIDE. [And again, this is provided that -march=armv6[t2] is deemed unsuitable for the purpose. I mean as it stands now -march=armv6[t2] produces code that is perfectly executable on armv7 processor such as Cortex-A8, but without NEON.]

There is one thing I seem to fail to clarify. I merely express my opinion on why I for one consider suggestions suboptimal. If somebody considers suggestion suitable, just approve...

@dot-asm

dot-asm commented Jun 23, 2018

Copy link
Copy Markdown
Contributor

And I believe the current solution is just unsafe.

I think @dot-asm implied that from the very beginning... the users have to know what they're doing, or they should simply not touch this stuff.

Well, these are formally "orthogonal" arguments. In sense that if solution is viewed unsafe, then we can't argue "they shouldn't be doing it", because we have to consider ill-minded user, one that would do it on purpose. I want to point out that I haven't actually dismissed(*) the idea of masking capability vectors, I only said that unmasked capability vector assignment does have value/application. At the same time I also said that I can see the value of masking too, just don't recognize significance. In other words in my mind it's "recognized value" on one hand vs. "believe it's just unsafe" on the other. And as long as the latter is formulated like that, I'll be inclined to choose former. And it does mean that if latter formulation was more substantial, I for one would weigh it differently.

(*) Biggest objection I have is OPENSS_*CAP_OVERRIDE as additional compile-time knob, and it has nothing to do with masking.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

Just in case that is not clear, with the additional compile-time options I have in mind the use case of
embedded systems, where you have exact knowledge of the CPU and complete control over the
compile options of every part of the software (firmware).

And what I don't understand, is why these few lines of code that are necessary to handle such
options, could create such an unacceptable maintenance burden.

@t-j-h

t-j-h commented Jun 25, 2018

Copy link
Copy Markdown
Member

This has nothing to do with set[e]uid handling at all - just supporting that viewpoint. And this mechanism has been used many times to solve issues at runtime for users.

@richsalz

Copy link
Copy Markdown
Contributor

Whew, long thread. This is not a 1.1.1-specific issue, so removing that milestone.

@richsalz richsalz removed this from the 1.1.1 milestone Jun 27, 2018
@mattcaswell mattcaswell added this to the Assessed milestone Jul 13, 2018
@bernd-edlinger
bernd-edlinger force-pushed the dont_trust_ia32cap_envvar_more_than_cpuid branch from d99aee6 to 810aa94 Compare August 20, 2019 09:54
@bernd-edlinger bernd-edlinger added branch: 1.1.1 Applies to OpenSSL_1_1_1-stable branch (EOL) branch: master Applies to master branch labels Aug 20, 2019
@bernd-edlinger

Copy link
Copy Markdown
Member Author

Rebased.
I still fail to see what is the value of using bits in environment variable without probing first,

Fixes test failures with illegal values in OPENSSL_ia32cap, i.e.:
OPENSSL_ia32cap=0xFFFFFFFFFFFFFFFF:0xFFFFFFFFFFFFFFFF make test

Add a configure option -DOPENSSL_IA32CAP_OVERRIDE=x:y

Add some documentation for OPENSSL_IA32CAP_OVERRRIDE
Fixes test failures with illegal values in OPENSSL_armcap, i.e.:
OPENSSL_armcap=0xFFFFFFFF make test

Add a configure option -DOPENSSL_ARMCAP_OVERRIDE=x
Fixes test failures with illegal values in OPENSSL_ppccap, i.e.:
OPENSSL_ppccap=0xFFFFFFFF make test

Add a configure option -DOPENSSL_PPCCAP_OVERRIDE=x
@bernd-edlinger

Copy link
Copy Markdown
Member Author

I consider this a security relevant issue.
I see no valid arguments against this.

@kroeckx

kroeckx commented Apr 12, 2020

Copy link
Copy Markdown
Member

I think this PR is trying to do multiple things at the same time:

  • Not allow the environment variable to turn on bits that have not been detected at run time
  • Have an other way to control it than the environment variables, and do it at compile time

Maybe this PR can be split in 2, so that we can discuss each part on it's own?

My understand is, that on some platforms you don't actually want to detect at runtime which instructions are supported by potentially running illegal instructions, because the platform doesn't have proper support for doing that. You want to be able to say at compile time which CPU features you want to support. Not building the assembler that's not supported is not enough, you want to force the use of those that are build without doing the runtime check.

@bernd-edlinger

Copy link
Copy Markdown
Member Author

The control by compile flags is just the beginning, in my road map,
I see also assembler modules switched on and off per compile flag,
just as the environment does. Imagine an embedded target, where
you know what CPU you have, why should you have Assembler
moules for CPU types that you know exactly will not be needed.

@t8m

t8m commented Apr 14, 2020

Copy link
Copy Markdown
Member

I am not sure this is the right approach. I can only repeat that to make it fully security irrelevant I would use ossl_safe_getenv(). There is no point in trying to outsmart the user who would need to already know details about his CPU when he is trying to set the environment variable.

@t8m

t8m commented Apr 14, 2020

Copy link
Copy Markdown
Member

To make it clear I was talking above about this change of the OPENSSL_xxcap semantics as is, not about build-time switching on/off assembler modules/code-paths. I would not be against that one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

branch: master Applies to master branch branch: 1.1.1 Applies to OpenSSL_1_1_1-stable branch (EOL)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants