Skip to content
Create new...
You have no unread notifications
/  ...  /  
Issues #11302  /  

ocamlc overwrites /dev/null #11302

Closed
thierry-martinez opened this issue Jun 7, 2022 · 8 comments · Fixed by #11412
Closed

ocamlc overwrites /dev/null #11302

thierry-martinez opened this issue Jun 7, 2022 · 8 comments · Fixed by #11412

Comments

@thierry-martinez

Contributor

thierry-martinez commented Jun 7, 2022

If root runs the following commands:

# echo 'print_endline "Hello, world!"' >hello.ml
# ocamlopt -o /dev/null hello.ml

if /dev/null was initially the null block device, the latter remains unchanged by these commands; whereas if root runs the following command instead:

# ocamlc -o /dev/null hello.ml

then /dev/null becomes a regular file (the outcome of the compilation).

@marc-chevalier initially reported this is thierry-martinez/stdcompat#18: I can (and will) fix this by using a workaround in stdcompat's ./configure, but I didn't expect ocamlc to have this behavior.

@nojb

Contributor

nojb commented Jun 7, 2022

Thanks for the report. The problem stems from this line

Misc.remove_file exec_name; (* avoid permission problems, cf PR#8354 *)

which deletes /dev/null (if you have sufficient permissions) before writing the output. This deletion was added in order to address #8354 (output file is left with existing permissions if it already exists on disk), so this needs to be looked at to figure out what's the best workaround.

@nojb

Contributor

nojb commented Jun 7, 2022

For comparison gcc sets the executable bit of the output file if it is a regular file that already exists, but not in the case of /dev/null, which it leaves untouched.

@xavierleroy

Contributor

For comparison gcc sets the executable bit of the output file if it is a regular file that already exists

This is a reasonable strategy, but we cannot implement it in ocamlc (currently) because the Sys portable interface doesn't give direct access to stat and to chmod, and I'd rather keep it this way. The question, then, is to find a minimal extension to Sys (or perhaps to Out_channel) that does what we want.

Let's not rush a fix. The "problem" has been with us since 2003, and sane users don't run compilers with superuser privileges.

@dra27

Member

dra27 commented Jun 7, 2022

In passing, #9787 proposed caml_sys_chmod as a primitive only (i.e. an external ocamlc could declare and use).

caml_sys_is_directory already uses stat for Sys.is_directory - would adding caml_sys_is_regular be OK? Sys.is_regular_file seems OK to add, and we could then keep the existing "delete if exists strategy" (which would avoid adding caml_sys_chmod and then pretending that it's not there in the stdlib)

@marc-chevalier

Contributor

I'm not saying what I was doing is sane (pretty much the opposite: fighting with a very non OCaml-friendly environment and build system), but there is now a common situation where we can expect users to run about anything as root: docker. Not saying it's a good idea, but the simple normal and common setup is to be root inside the container, as far as I know.

@dra27

Member

dra27 commented Jun 8, 2022

there is now a common situation where we can expect users to run about anything as root: docker. Not saying it's a good idea, but the simple normal and common setup is to be root inside the container, as far as I know.

It certainly doesn't need to be that way - deploying in Docker as root is one thing, but using it as a development environment, one should create a non-privileged user in the normal way. cf. the opam images at ocaml/opam on Docker hub which all have an opam normal user.

xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Jul 7, 2022
xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Jul 7, 2022
xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Jul 7, 2022
xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Jul 7, 2022
@xavierleroy

Contributor

Sys.is_regular_file seems OK to add,

Agreed. I had a more complicated fix in mind, but here is a fix based on Sys.is_regular_file: #11412.

@xavierleroy

Contributor

xavierleroy commented Jul 7, 2022

For the record, the alternate solution I had in mind is here: xavierleroy@d898922 . The good thing about this alternate solution is that the bytecode file generated by ocamlc is not executable until it's complete and ready for execution. The not so good thing is that the final permissions are harder to predict.

xavierleroy added a commit to xavierleroy/ocaml that referenced this issue Jul 13, 2022
xavierleroy added a commit that referenced this issue Jul 13, 2022
@tryashtar
Add heading text Add bold text, <Ctrl+b> Add italic text, <Ctrl+i>
Add a quote, <Ctrl+Shift+.> Add code, <Ctrl+e> Add a link, <Ctrl+k>
Add a bulleted list, <Ctrl+Shift+8> Add a numbered list, <Ctrl+Shift+7> Add a task list, <Ctrl+Shift+l>
Directly mention a user or team Reference an issue, pull request, or discussion
Select a reply ctrl .
Add saved reply
Remember, contributions to this repository should follow its contributing guidelines and code of conduct.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants