You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
local privilege escalation issue with ocaml binaries #7557
Original bug ID: 7557 Reporter: emilliken Assigned to:@damiendoligez Status: resolved (set by @damiendoligez on 2017-06-23T15:19:47Z) Resolution: fixed Priority: urgent Severity: major OS: *nix Version: 4.04.0 Target version: 4.04.2 Fixed in version: 4.04.2 Category: runtime system and C interface Monitored by:@gasche
Bug description
I've marked this issue as private.
By default, it seems the environment variables CAML_CPLUGINS, CAML_NATIVE_CPLUGINS, and CAML_BYTE_CPLUGINS can be used to auto-load code into any executable produced with ocamlopt or ocamlc. This can lead to privilege escalation if the binary is marked setuid.
@emilliken - thanks for the report, and for your sensitivity in not marking it public. Can I ask whether you've found this as a result of a security review of the code, or as part of a production system?
This is a serious issue indeed, many thanks for reporting it.
I think the first thing to do is to harden the existing plugin mechanism along the lines suggested by the reporter, i.e. check the effective and real user ids.
Then we should (re-)discuss whether the plugin mechanism should be on by default or off by default with a configure-time flag to activate it.
It might be worth using issetugid on the systems that provide it (I think Solaris, MacOs and the BSDs). It's slightly more robust than the uid == euid check since it will still trigger even if the original program does something like setuid(0).
On the administrative side, I don't think we can avoid OCaml's second CVE: we need to disclose that 4.04.0 and 4.04.1 are vulnerable, and presumably we will need to release this fix as 4.04.2 or at least provide a back-port patch for 4.04.1?
Using issetugid(2) is definitely recommended, when available. While checking current and real uid works to some extent, it doesn't cover the case of existing file handles that are privileged. For instance, is the C plugin code possible to trigger when linking an OCaml program as a library?
The OpenBSD implementation of issetugid is an instance of a non-uid based check; on that operating system it is a taint flag set at exec time. http://man.openbsd.org/OpenBSD-6.1/issetugid.2
@avsm I don't follow what you mean about "existing file handles that are privileged" (presumably in the case where real uid = effective uid). Can you elaborate?
The get[e]uid calls only return the current state of the system, and so the library cannot determine what happened previously from the calling code with respect to the uids. Therefore, the process may already have file descriptors open that would be considered privileged (i.e. they were opened with root-level access).
To cover this case, some issetugid(2) implementations such as OpenBSD "taint" the process if it was ever made setuid or setgid in any previous execve() system calls. This is not something that is easy to recreate from within a library, and hence it is better to call the function if available.
I believe that glibc/musl libc propagate the libc secure flag that lpw25 posted above, and do not do such taint tracking. Therefore we probably need to analyse what the scope of the C plugin code is -- can it be triggered when OCaml is used as a library, or is it only from within ocamlopt-generated executables before any OCaml code is called?
@avsm Each solution comes with a different trade-off: ease-of-use vs performance vs portability, etc. If you are looking for portability and ease-of-use, the c-plugin mechanism is probably the best currently available for OCaml (from my experience of playing with perf and LD_PRELOAD in the past).
There is also a problem with TMPDIR, but it has been around forever and seems much less critical: typically, programs would create temporary files through Unix.open_temp_file, which would not overwrite an existing file.
No, the security hole with TMPDIR is critical. The issue is not the ability to overwrite an existing file (as you say, open_temp_file will not do this). The issue is that there are several directories on most Unix systems whose contents are habitually executed by root (among many others /etc/init.d, /etc/cron.d, /etc/bash_completion.d). If an attacker can create a file in such directories, of any filename but with attacker-controlled contents, then the attacker can execute arbitrary code as root.
@stedolan Good point. There is still a difference between the security hole created by CPLUGINS against which the author of the program cannot do anything, and the one related to TMPDIR, for which the author can (and should anyway) explicitly choose the directory (through Filename.set_temp_dir_name or ~temp_dir) if the programs needs to create temporary files.
I agree. The TMPDIR hole makes it difficult to write a secure suid program that uses Filename.open_temp_file, while the CPLUGINS hole makes it impossible to write a secure suid program at all.
Do you think that it would be possible to put some process in place that would help avoid similar issues in the future, without having too much of a bureaucracy cost?
The only simple thing that I can see right now is: make it an habit that any change that impacts the runtime system has someone thinking about security considerations before the merge decision. That is, not merge until someone explicitly wrote a comment on the PR to the effect of "I reviewed the security implication of the patch and I believe it is safe".
("impacts the runtime system" is a rather broad category. Is is broad enough? Is it too broad? I don't see an effective and simple criterion that would be better.)
(I could try to enforce this in a semi-automated way by asking the person that ticks the "security impact considered" box to add a github label, but my previous adventure with the no-changes-required label suggests that this is fragile in ways I don't completely understand. I would be interested in hearing people's thoughts about the adequate process, rather than how to automate it, first.)
Let's keep this PR focused on the technical issue that is described in the CVE. Meta-discussions on how to prevent similar blunders in the future should take place elsewhere, e.g. on the Caml developers list.
Activity
vicuna commentedon Jun 15, 2017
Comment author: @lefessan
Another fix would be to use "./configure --no-cplugins" when building OCaml, to completely avoid cplugins when creating setuid executables.
Maybe we could also create a "secure" runtime variant, that would disable cplugins, and maybe other features seen as security weaknesses.
vicuna commentedon Jun 15, 2017
Comment author: @dra27
@lefessan - just in case you were planning to, please don't push patches to GitHub at this stage.
vicuna commentedon Jun 15, 2017
Comment author: @dra27
@emilliken - thanks for the report, and for your sensitivity in not marking it public. Can I ask whether you've found this as a result of a security review of the code, or as part of a production system?
vicuna commentedon Jun 15, 2017
Comment author: @mshinwell
Leo and I looked at this a little and we suspect that checking the real and effective uids is the right solution.
I think I would be in favour of some kind of "secure" runtime variant (but shouldn't that be the default)?
vicuna commentedon Jun 15, 2017
Comment author: @dra27
It's certainly surprised me that this undocumented feature was enabled by default - unless you'd seen #668, I'm not clear how you'd know it was there?
vicuna commentedon Jun 15, 2017
Comment author: @xavierleroy
This is a serious issue indeed, many thanks for reporting it.
I think the first thing to do is to harden the existing plugin mechanism along the lines suggested by the reporter, i.e. check the effective and real user ids.
Then we should (re-)discuss whether the plugin mechanism should be on by default or off by default with a configure-time flag to activate it.
vicuna commentedon Jun 15, 2017
Comment author: @lpw25
It might be worth using issetugid on the systems that provide it (I think Solaris, MacOs and the BSDs). It's slightly more robust than the uid == euid check since it will still trigger even if the original program does something like setuid(0).
vicuna commentedon Jun 15, 2017
Comment author: @dra27
On the administrative side, I don't think we can avoid OCaml's second CVE: we need to disclose that 4.04.0 and 4.04.1 are vulnerable, and presumably we will need to release this fix as 4.04.2 or at least provide a back-port patch for 4.04.1?
vicuna commentedon Jun 15, 2017
Comment author: @lpw25
For reference, here is the code in glibc which effectively guards LD_PRELOAD from similar issues:
__libc_enable_secure = (__geteuid () != __getuid ()
|| __getegid () != __getgid ());
which is in elf/enbl-secure.c.
vicuna commentedon Jun 15, 2017
Comment author: @avsm
Using issetugid(2) is definitely recommended, when available. While checking current and real uid works to some extent, it doesn't cover the case of existing file handles that are privileged. For instance, is the C plugin code possible to trigger when linking an OCaml program as a library?
The OpenBSD implementation of issetugid is an instance of a non-uid based check; on that operating system it is a taint flag set at exec time. http://man.openbsd.org/OpenBSD-6.1/issetugid.2
vicuna commentedon Jun 15, 2017
Comment author: @mshinwell
@avsm I don't follow what you mean about "existing file handles that are privileged" (presumably in the case where real uid = effective uid). Can you elaborate?
vicuna commentedon Jun 15, 2017
Comment author: @avsm
The get[e]uid calls only return the current state of the system, and so the library cannot determine what happened previously from the calling code with respect to the uids. Therefore, the process may already have file descriptors open that would be considered privileged (i.e. they were opened with root-level access).
To cover this case, some issetugid(2) implementations such as OpenBSD "taint" the process if it was ever made setuid or setgid in any previous execve() system calls. This is not something that is easy to recreate from within a library, and hence it is better to call the function if available.
I believe that glibc/musl libc propagate the libc secure flag that lpw25 posted above, and do not do such taint tracking. Therefore we probably need to analyse what the scope of the C plugin code is -- can it be triggered when OCaml is used as a library, or is it only from within ocamlopt-generated executables before any OCaml code is called?
vicuna commentedon Jun 15, 2017
Comment author: emilliken
dra - this was found after ltrace'ing my ocaml binary and tracing back some getenv calls. It was not found as part of a production system.
vicuna commentedon Jun 15, 2017
Comment author: @dra27
@emilliken - thanks. It doesn't lessen the seriousness, but it's useful to know that it's not yet been found being exploited!
vicuna commentedon Jun 15, 2017
Comment author: @xavierleroy
Perhaps something along the lines of:
38 remaining items
vicuna commentedon Jun 23, 2017
Comment author: @damiendoligez
@Frisch
Yes but we often get surprised by the creativity of attackers.
Anyway, do we have any actual example of a setuid program written in OCaml? I'd rather be more secure and risk breaking them (if they exist).
@lefessan, @avsm
Please do open a new PR to discuss the future of c-plugins.
vicuna commentedon Jun 23, 2017
Comment author: @lefessan
@avsm Each solution comes with a different trade-off: ease-of-use vs performance vs portability, etc. If you are looking for portability and ease-of-use, the c-plugin mechanism is probably the best currently available for OCaml (from my experience of playing with
perfandLD_PRELOADin the past).vicuna commentedon Jun 23, 2017
Comment author: @stedolan
@Frish
No, the security hole with TMPDIR is critical. The issue is not the ability to overwrite an existing file (as you say, open_temp_file will not do this). The issue is that there are several directories on most Unix systems whose contents are habitually executed by root (among many others /etc/init.d, /etc/cron.d, /etc/bash_completion.d). If an attacker can create a file in such directories, of any filename but with attacker-controlled contents, then the attacker can execute arbitrary code as root.
vicuna commentedon Jun 23, 2017
Comment author: @alainfrisch
@stedolan Good point. There is still a difference between the security hole created by CPLUGINS against which the author of the program cannot do anything, and the one related to TMPDIR, for which the author can (and should anyway) explicitly choose the directory (through Filename.set_temp_dir_name or ~temp_dir) if the programs needs to create temporary files.
vicuna commentedon Jun 23, 2017
Comment author: @damiendoligez
This PR is now public
vicuna commentedon Jun 23, 2017
Comment author: @damiendoligez
Fixed in 4.04.2, cherry-picked to 4.05.0 and trunk.
Trunk version adds Unix.unsafe_getenv; 4.04 and 4.05 don't.
vicuna commentedon Jun 23, 2017
Comment author: @stedolan
@Frisch
I agree. The TMPDIR hole makes it difficult to write a secure suid program that uses Filename.open_temp_file, while the CPLUGINS hole makes it impossible to write a secure suid program at all.
vicuna commentedon Jun 23, 2017
Comment author: @gasche
Do you think that it would be possible to put some process in place that would help avoid similar issues in the future, without having too much of a bureaucracy cost?
The only simple thing that I can see right now is: make it an habit that any change that impacts the runtime system has someone thinking about security considerations before the merge decision. That is, not merge until someone explicitly wrote a comment on the PR to the effect of "I reviewed the security implication of the patch and I believe it is safe".
("impacts the runtime system" is a rather broad category. Is is broad enough? Is it too broad? I don't see an effective and simple criterion that would be better.)
(I could try to enforce this in a semi-automated way by asking the person that ticks the "security impact considered" box to add a github label, but my previous adventure with the no-changes-required label suggests that this is fragile in ways I don't completely understand. I would be interested in hearing people's thoughts about the adequate process, rather than how to automate it, first.)
vicuna commentedon Jun 24, 2017
Comment author: @xavierleroy
Let's keep this PR focused on the technical issue that is described in the CVE. Meta-discussions on how to prevent similar blunders in the future should take place elsewhere, e.g. on the Caml developers list.