nix icon indicating copy to clipboard operation
nix copied to clipboard

Add the Linux prctl syscall

Open DOhlsson opened this issue 4 years ago • 12 comments
trafficstars

I have added support for the prctl syscall.

However, I am not sure that this is the right way to do it. Another way would be to wrap each possible flag to prctl as it's own function, that way it would be more similar to how the prctl crate does it. Wrapping the different options would be safer, since some options to prctl return data through pointers.

What do you think?

DOhlsson avatar Oct 03 '21 19:10 DOhlsson

I forgot to mention the related issue (#601) in my first post.

To answer your first question, I think that it would be nice to have prctl implemented in Nix. There are other crates for inotify and epoll too, but if I can get all three of them in one crate then that is my preferred option.

I agree that my pull request isn't very good. But I am willing to put in the work to bring it up to Nix's standards.

The weird thing about prctl is that some options can return data through the function call and other options return data to the location pointed to by one of the arguments, this behaviour makes it quite unsafe. Hence why I think that making wrapper functions is the best option.

I'll work a bit on this PR and come back with the better solution.

DOhlsson avatar Oct 05 '21 20:10 DOhlsson

What is your opinion on the solution now? I have not implemented all the options yet as I wanted to hear your opinion first.

Possibly the prctl function and PrctlOption enum do not have to be public anymore, if all possibilities are implemented as separate functions.

DOhlsson avatar Jan 06 '22 16:01 DOhlsson

Thank you for your feedback! I have a couple more of the options to add (and fix the tests), but I hope to get some time to get it done this week.

What is your opinion on the more exotic options that are available through prctl, should I implement them as well in this PR? Like architecture specific options, that I don't have the hardware to test with (powerpc, ia, mips etc.) and options to control capabilities where there are recommended higher-level libraries like libcap.

I have implemented the process-control options and left out the more exotic options for now.

DOhlsson avatar Mar 27 '22 20:03 DOhlsson

@asomers I can't seem to figure out what is going wrong in the builds. Do you have any idea? Could you point me in the right direction?

The errors are in architectures I don't have access to, namely MIPS, MIPS64, mipsel, arm gnueabi and armv7 gnueabihf. It appears to be pointer related, as the error EFAULT from prctl indicates that it is an invalid address.

I pass the pointer to prctl as a &mut c_int cast as c_ulong so there might be something architecture specific that I do not know.

Do you know of any examples in the rest of the Nix code where a pointer is passed to and written by the syscall?

DOhlsson avatar Apr 10 '22 14:04 DOhlsson

uclibceabihf is failing through no fault of your own, and hopefully it will soon be fixed on the master branch. arm gnueabi and armv7 gnueabihf are tested using QEMU. QEMU doesn't implement all syscalls. If prctl is one, then you can add #[cfg_attr(qemu, ignore)] to those test cases.

asomers avatar Apr 11 '22 04:04 asomers

@asomers I have fixed the tests and completed a bunch more options. I think this PR is ready if you are satisfied with it.

DOhlsson avatar May 14 '22 11:05 DOhlsson

@asomers What do you think? Is this PR ready or is there anything I should change?

DOhlsson avatar Jul 09 '22 18:07 DOhlsson

I'm interested in this too, particularly setting no new privileges.

ColonelThirtyTwo avatar Jul 09 '22 18:07 ColonelThirtyTwo

All of the tests seem to assume that the default settings will be present. Are you sure that will be true, or could it depend on the environment?

I have changed the tests so that they do not assume that the defaults are set. And I've also added cleanup that resets the settings to what they were before the test.

Also, is it possible for any of them to interfere with other tests, including in other modules? It makes me nervous that they all change global settings.

Some of these affect the process and not only the thread, so it might be possible that these tests affect other tests. I'll move the tests into their own test target.

DOhlsson avatar Jul 10 '22 17:07 DOhlsson

I see, you did move them into their own process. That looks good. Would you mind squashing your commits now?

asomers avatar Jul 10 '22 22:07 asomers

@asomers I have squashed the commits. I also added a line to the changelog.

DOhlsson avatar Jul 11 '22 06:07 DOhlsson

@asomers is it ready to merge or is there anything else I should do?

DOhlsson avatar Jul 28 '22 20:07 DOhlsson

@asomers I have rebased this branch onto latest commit from master. Can this PR be merged as is, or would you like me to change anything else?

DOhlsson avatar Aug 27 '23 18:08 DOhlsson