{"id":608,"date":"2012-03-26T13:17:09","date_gmt":"2012-03-26T21:17:09","guid":{"rendered":"http:\/\/www.outflux.net\/blog\/?p=608"},"modified":"2012-03-28T12:39:38","modified_gmt":"2012-03-28T20:39:38","slug":"keeping-your-process-unprivileged","status":"publish","type":"post","link":"https:\/\/outflux.net\/blog\/archives\/2012\/03\/26\/keeping-your-process-unprivileged\/","title":{"rendered":"keeping your process unprivileged"},"content":{"rendered":"<p>One of the prerequisites for <a href=\"http:\/\/www.outflux.net\/blog\/archives\/2012\/03\/22\/seccomp-filter-now-in-ubuntu\/\">seccomp filter<\/a> is the new <a href=\"http:\/\/kernel.ubuntu.com\/git?p=ubuntu\/ubuntu-precise.git;a=commitdiff;h=8e8079f9e951ef1921b0de648568ea4b10c38d8b\"><code>PR_SET_NO_NEW_PRIVS<\/code> prctl<\/a> from Andy Lutomirski.<\/p>\n<p>If you&#8217;re not interested in digging into creating a seccomp filter for your program, but you know your program should be effectively a &#8220;leaf node&#8221; in the process tree, you can call PR_SET_NO_NEW_PRIVS (nnp) to make sure that the current process and its children can not gain new privileges (like through running a setuid binary). This produces some fun results, since things like the &#8220;ping&#8221; tool expect to gain enough privileges to open a raw socket. If you set nnp to &#8220;1&#8221;, suddenly that can&#8217;t happen any more.<\/p>\n<p>Here&#8217;s a quick example that sets nnp, and tries to run the command line arguments:<\/p>\n<pre class=\"brush:c\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;sys\/prctl.h&gt;\r\n#ifndef PR_SET_NO_NEW_PRIVS\r\n# define PR_SET_NO_NEW_PRIVS 38\r\n#endif\r\n\r\nint main(int argc, char * argv[])\r\n{\r\n        if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {\r\n                perror(\"prctl(NO_NEW_PRIVS)\");\r\n                return 1;\r\n        }\r\n\r\n        return execvp(argv[1], &argv[1]);\r\n}\r\n<\/pre>\n<p>When it tries to run ping, the setuid-ness just gets ignored:<\/p>\n<pre class=\"brush:shell\">\r\n$ gcc -Wall nnp.c -o nnp\r\n$ .\/nnp ping -c1 localhost\r\nping: icmp open socket: Operation not permitted\r\n<\/pre>\n<p>So, if your program has all the privs its going to need, consider using nnp to keep it from being a potential gateway to more trouble. Hopefully we can ship something like this trivial nnp helper as part of coreutils or similar, like nohup, nice, etc.<\/p>\n<p style='text-align:left'>&copy; 2012, <a href=\"https:\/\/outflux.net\/blog\/\">Kees Cook<\/a>. This work is licensed under a <a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\">Creative Commons Attribution-ShareAlike 4.0 License<\/a>.<br \/><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><img decoding=\"async\" alt=\"CC BY-SA 4.0\" style=\"border-width:0\" src=\"https:\/\/i.creativecommons.org\/l\/by-sa\/4.0\/88x31.png\" \/><\/a> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the prerequisites for seccomp filter is the new PR_SET_NO_NEW_PRIVS prctl from Andy Lutomirski. If you&#8217;re not interested in digging into creating a seccomp filter for your program, but you know your program should be effectively a &#8220;leaf node&#8221; in the process tree, you can call PR_SET_NO_NEW_PRIVS (nnp) to make sure that the current [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,20,18,6,14,19],"tags":[],"_links":{"self":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/608"}],"collection":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/comments?post=608"}],"version-history":[{"count":9,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/608\/revisions"}],"predecessor-version":[{"id":617,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/posts\/608\/revisions\/617"}],"wp:attachment":[{"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/media?parent=608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/categories?post=608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/outflux.net\/blog\/wp-json\/wp\/v2\/tags?post=608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}