# Start use capabilities on linux

**URL:** <https://www.ruby-forum.com/t/start-use-capabilities-on-linux/163170>\
**Category:** NGINX\
**Created:** [March 17, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/start-use-capabilities-on-linux/163170 "2009-03-17T22:45:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirill\_ASKorinskiy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@Kirill\_ASKorinskiy](https://www.ruby-forum.com/u/Kirill_ASKorinskiy)\
**Post date:** [March 17, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/start-use-capabilities-on-linux/163170/1 "2009-03-17T22:45:06Z")

</div>

From: Kirill A. Korinskiy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

## The nginx required privilege mode only on master process and only bind ports \<1024. In linux proccess can bind ports \<1024 in not privilege mode if the process does capset(CAP\_NET\_BIND\_SERVICE).

auto/lib/capabilities/conf | 20 +++++++++++++  
auto/os/linux | 3 ++  
src/core/nginx.c | 7 +++±  
src/core/ngx\_cycle.c | 1 -  
src/os/unix/ngx\_process\_cycle.c | 22 +±-----------  
src/os/unix/ngx\_user.c | 61  
+++++++++++++++++++++++++++++++++++++++  
src/os/unix/ngx\_user.h | 14 +++++++++  
7 files changed, 107 insertions(+), 21 deletions(-)  
create mode 100644 auto/lib/capabilities/conf

diff --git a/auto/lib/capabilities/conf b/auto/lib/capabilities/conf  
new file mode 100644  
index  
0000000000000000000000000000000000000000…484401c045a67cc0be523f64de2f177b14cae97b  
— /dev/null  
+++ b/auto/lib/capabilities/conf  
@@ -0,0 +1,20 @@  
+  
+# Copyright (C) Kirill A. Korinskiy  
+  
+  
+ngx\_feature=“capabilities”  
+ngx\_feature\_name=“NGX\_HAVE\_CAPABILITIES”  
+ngx\_feature\_run=no  
+ngx\_feature\_incs=“#include \<sys/capability.h\>”  
+ngx\_feature\_path=  
+ngx\_feature\_libs=“-lcap”  
+ngx\_feature\_test="cap\_user\_header\_t header;

- cap\_user\_data\_t cap;
- capset(header, cap);
- return -1;"  
+. auto/feature
- 
- 

+if [$ngx\_found = yes]; then

- CORE\_LIBS=“$CORE\_LIBS $ngx\_feature\_libs”  
+fi  
diff --git a/auto/os/linux b/auto/os/linux  
index  
3c2c8419d58cafcfafb93d30032cc7c8553cccb8…e4e036c2aaafb59c174e665a4da9a2b2db8c3e2b  
100644  
— a/auto/os/linux  
+++ b/auto/os/linux  
@@ -121,3 +121,6 @@ ngx\_feature\_libs=  
ngx\_feature\_test=“long mask = 0;  
sched\_setaffinity(0, 32, (cpu\_set\_t \*) &mask)”  
. auto/feature
- 

+# capabilities  
+. auto/lib/capabilities/conf  
diff --git a/src/core/nginx.c b/src/core/nginx.c  
index  
e6ebae54431f074fbc41974c35c44ac9ab4492cc…78e8227b8b1425a6aa7df65cc0d36f67f20ad7d9  
100644  
— a/src/core/nginx.c  
+++ b/src/core/nginx.c  
@@ -322,6 +322,12 @@ main(int argc, char \*const \*argv)

```
 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, 

```

ngx\_core\_module);

+#if (NGX\_HAVE\_CAPABILITIES)

- if (geteuid() == 0) {
- ngx\_set\_capabilities(cycle);
- }  
+#endif
- ngx\_process = ccf-\>master ? NGX\_PROCESS\_MASTER :  
NGX\_PROCESS\_SINGLE;

## #if (NGX\_WIN32) @@ -334,7 +340,6 @@ main(int argc, char \*const \*argv) if (ngx\_service(cycle-\>log) != NGX\_OK) { return 1; }

```
     return 0;
 }

```

#endif  
diff --git a/src/core/ngx\_cycle.c b/src/core/ngx\_cycle.c  
index  
f3dc8619b484c9727770233b26e6580ca32fda80…ce8de0dd6d827235ace0c7f328d0db2c604af070  
100644  
— a/src/core/ngx\_cycle.c  
+++ b/src/core/ngx\_cycle.c  
@@ -14,7 +14,6 @@ static void ngx\_destroy\_cycle\_pools(ngx\_conf\_t \*conf);  
static ngx\_int\_t ngx\_cmp\_sockaddr(struct sockaddr \*sa1, struct sockaddr  
\*sa2);  
static void ngx\_clean\_old\_cycles(ngx\_event\_t \*ev);

- 

volatile ngx\_cycle\_t \*ngx\_cycle;  
ngx\_array\_t ngx\_old\_cycles;

diff --git a/src/os/unix/ngx\_process\_cycle.c  
b/src/os/unix/ngx\_process\_cycle.c  
index  
a14a03c748dd2793b5e2ea0b18f709267df01d3e…f0950e64a353d1c46c32bbb97964c7924b67e813  
100644  
— a/src/os/unix/ngx\_process\_cycle.c  
+++ b/src/os/unix/ngx\_process\_cycle.c  
@@ -854,27 +854,11 @@ ngx\_worker\_process\_init(ngx\_cycle\_t \*cycle,  
ngx\_uint\_t priority)  
}  
#endif

+#ifndef NGX\_HAVE\_CAPABILITIES  
if (geteuid() == 0) {

- 

```
   if (setgid(ccf->group) == -1) {

```

- 

```
       ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                     "setgid(%d) failed", ccf->group);

```

- 

```
       /* fatal */

```

- 

```
       exit(2);

```

- 

```
   }

```

- 
- 

```
   if (initgroups(ccf->username, ccf->group) == -1) {

```

- 

```
       ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                     "initgroups(%s, %d) failed",

```

- 

```
                     ccf->username, ccf->group);

```

- 

```
   }

```

- 
- 

```
   if (setuid(ccf->user) == -1) {

```

- 

```
       ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                     "setuid(%d) failed", ccf->user);

```

- 

```
       /* fatal */

```

- 

```
       exit(2);

```

- 

```
   }

```

- 

```
   ngx_switch_user(cycle);

```

}  
+#endif

#if (NGX\_HAVE\_SCHED\_SETAFFINITY)

diff --git a/src/os/unix/ngx\_user.c b/src/os/unix/ngx\_user.c  
index  
4bad1c3070e517fadb53007be7f90acb576fba4f…d5ebb6c3e22b96d66a5e816dbfa860635fa7a3d8  
100644  
— a/src/os/unix/ngx\_user.c  
+++ b/src/os/unix/ngx\_user.c  
@@ -7,6 +7,10 @@  
#include \<ngx\_config.h\>  
#include \<ngx\_core.h\>

+#if (NGX\_HAVE\_CAPABILITIES)  
+#include \<sys/capability.h\>  
+#endif  
+

/\*

- Solaris has thread-safe crypt()  
@@ -106,3 +110,60 @@ ngx\_crypt(ngx\_pool\_t \*pool, u\_char \*key, u\_char  
\*salt, u\_char \*\*encrypted)  
#endif

#endif /\* NGX\_CRYPT \*/  
+  
+void ngx\_switch\_user(ngx\_cycle\_t \*cycle)  
+{

- ngx\_core\_conf\_t \*ccf;
- 
- ccf = (ngx\_core\_conf\_t \*) ngx\_get\_conf(cycle-\>conf\_ctx,  
ngx\_core\_module);
- 
- if (setgid(ccf-\>group) == -1) {
- 

```
   ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                 "setgid(%d) failed", ccf->group);

```

- 

```
   /* fatal */

```

- 

```
   exit(2);

```

- }
- 
- if (initgroups(ccf-\>username, ccf-\>group) == -1) {
- 

```
   ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                 "initgroups(%s, %d) failed",

```

- 

```
                 ccf->username, ccf->group);

```

- }
- 
- if (setuid(ccf-\>user) == -1) {
- 

```
   ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                 "setuid(%d) failed", ccf->user);

```

- 

```
   /* fatal */

```

- 

```
   exit(2);

```

- }  
+}
- 

+#if (NGX\_HAVE\_CAPABILITIES)  
+  
+void ngx\_set\_capabilities(ngx\_cycle\_t \*cycle)  
+{

- ngx\_core\_conf\_t \*ccf;
- 
- struct \_\_user\_cap\_header\_struct header;
- struct \_\_user\_cap\_data\_struct cap;
- 
- ccf = (ngx\_core\_conf\_t \*) ngx\_get\_conf(cycle-\>conf\_ctx,  
ngx\_core\_module);
- 
- prctl(PR\_SET\_KEEPCAPS, 1, 0, 0, 0);
- 
- ngx\_switch\_user(cycle);
- 
- header.version = \_LINUX\_CAPABILITY\_VERSION;
- header.pid = 0;
- 
- cap.effective = cap.permitted = (1 \<\< CAP\_NET\_BIND\_SERVICE);
- 
- if (capset(&header, &cap) == -1) {
- 

```
   ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                 "capset(%d) failed", ccf->user);

```

- 

```
   /* fatal */

```

- 

```
   exit(2);

```

- }  
+}
- 

+#endif  
diff --git a/src/os/unix/ngx\_user.h b/src/os/unix/ngx\_user.h  
index  
a24a66bf34ead7e9a1f8007e1c4be785601a937c…1ea5e360219af2dab4da5df1811c3a30eb783821  
100644  
— a/src/os/unix/ngx\_user.h  
+++ b/src/os/unix/ngx\_user.h  
@@ -15,10 +15,24 @@  
typedef uid\_t ngx\_uid\_t;  
typedef gid\_t ngx\_gid\_t;

+#if (NGX\_CRYPT)  
+  
+#if (NGX\_HAVE\_GNU\_CRYPT\_R)  
+

ngx\_int\_t ngx\_crypt(ngx\_pool\_t \*pool, u\_char \*key, u\_char \*salt,  
u\_char \*\*encrypted);

+#endif  
+  
+#endif /\* NGX\_CRYPT \*/  
+  
+void ngx\_switch\_user(ngx\_cycle\_t \*cycle);  
+  
+#if (NGX\_HAVE\_CAPABILITIES)  
+  
+void ngx\_set\_capabilities(ngx\_cycle\_t \*cycle);

+#endif

#endif /\* _NGX\_USER\_H\_INCLUDED_ \*/

---

<div class="post-metadata">

**Author:** ![Kirill\_ASKorinskiy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@Kirill\_ASKorinskiy](https://www.ruby-forum.com/u/Kirill_ASKorinskiy)\
**Post date:** [March 18, 2009, 2:30am UTC](https://www.ruby-forum.com/t/start-use-capabilities-on-linux/163170/2 "2009-03-18T02:30:56Z")

</div>

Hello!

On Wed, Mar 18, 2009 at 12:36:47AM +0300, Kirill A. Korinskiy wrote:

> From: Kirill A. Korinskiy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> The nginx required privilege mode only on master process and only bind  
> ports \<1024. In linux proccess can bind ports \<1024 in not privilege  
> mode if the process does capset(CAP\_NET\_BIND\_SERVICE).

Note that using root for master process needed not only for  
bind(), but also to access restricted configuration files (e.g.  
private keys) during reconfiguration. So dropping root from  
master should be at least configurable.

It’s also not clear what will happen on binary upgrade. Looks  
like with current code capabilities will be lost on exec() and  
upgraded binary won’t be able to bind() privileged ports anymore.  
But I’m not really familiar will linux capabilites interface, so I  
may be wrong.

Not even mentioning you are dropping root before writing pidfile.  
🙂

Also there is a couple of unrelated changes and some whitespace  
damage/style violations, but it doesn’t really matter.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Kirill\_ASKorinskiy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@Kirill\_ASKorinskiy](https://www.ruby-forum.com/u/Kirill_ASKorinskiy)\
**Post date:** [March 18, 2009, 7:10am UTC](https://www.ruby-forum.com/t/start-use-capabilities-on-linux/163170/3 "2009-03-18T07:10:28Z")

</div>

On Wed, Mar 18, 2009 at 04:23:48AM +0300, Maxim D. wrote:

> Note that using root for master process needed not only for  
> Not even mentioning you are dropping root before writing pidfile.  
> 🙂

The root privileges is also required to rotate logs if they are in  
a directories where workers can not write: master opens files and  
chown/chmod() them.

---

<div class="post-metadata">

**Author:** ![Kirill\_ASKorinskiy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@Kirill\_ASKorinskiy](https://www.ruby-forum.com/u/Kirill_ASKorinskiy)\
**Post date:** [March 18, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/start-use-capabilities-on-linux/163170/4 "2009-03-18T16:52:41Z")

</div>

From: Kirill A. Korinskiy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

capset(CAP\_CHOWN) grand the proccess rule to change arbitrary to file.

CAP\_DAC\_OVERRIDE and CAP\_DAC\_READ\_SEARCH bypass file read, write and  
execute permission checks.

## CAP\_SETUID need for rewert back to privileged processes (whose effective user ID is 0).

src/os/unix/ngx\_process.c | 9 +++++++++  
src/os/unix/ngx\_user.c | 13 ++++++++++±-  
2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/os/unix/ngx\_process.c b/src/os/unix/ngx\_process.c  
index  
18b2601aff17895cc250e54621651d72aee09790..9f06fdfa460ceadac032a62aae18559238fc17c6  
100644  
— a/src/os/unix/ngx\_process.c  
+++ b/src/os/unix/ngx\_process.c  
@@ -247,6 +247,15 @@ ngx\_execute\_proc(ngx\_cycle\_t \*cycle, void \*data)  
{  
ngx\_exec\_ctx\_t \*ctx = data;

+#if (NGX\_HAVE\_CAPABILITIES)

- if (setuid(0) == -1) {

- 

```
   ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                 "setuid(0) failed");

```

- 

```
   /* fatal */

```

- 

```
   exit(2);

```

- }  
+#endif

- if (execve(ctx-\>path, ctx-\>argv, ctx-\>envp) == -1) {  
ngx\_log\_error(NGX\_LOG\_ALERT, cycle-\>log, ngx\_errno,  
“execve() failed while executing %s "%s"”,  
diff --git a/src/os/unix/ngx\_user.c b/src/os/unix/ngx\_user.c  
index  
d5ebb6c3e22b96d66a5e816dbfa860635fa7a3d8..d3f6b5bd1d7d379ff4de9418955bcc285f0696d4  
100644  
— a/src/os/unix/ngx\_user.c  
+++ b/src/os/unix/ngx\_user.c  
@@ -149,14 +149,23 @@ void ngx\_set\_capabilities(ngx\_cycle\_t \*cycle)

- prctl(PR\_SET\_KEEPCAPS, 1, 0, 0, 0);

- if (prctl(PR\_SET\_KEEPCAPS, 1, 0, 0, 0) == -1) {

- 

```
   ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                 "prctl(%d) failed", ccf->user);

```

- 

```
   /* fatal */

```

- 

```
   exit(2);

```

- }

- cap.effective = cap.permitted = (1 \<\< CAP\_NET\_BIND\_SERVICE);

- cap.effective = cap.permitted = (1 \<\< CAP\_NET\_BIND\_SERVICE |

- 

```
        1 << CAP_CHOWN |

```

- 

```
        1 << CAP_DAC_OVERRIDE |

```

- 

```
        1 << CAP_DAC_READ_SEARCH |

```

- 

```
        1 << CAP_SETUID);

```

if (capset(&header, &cap) == -1) {  
ngx\_log\_error(NGX\_LOG\_EMERG, cycle-\>log, ngx\_errno,

---

<div class="post-metadata">

**Author:** ![Kirill\_ASKorinskiy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@Kirill\_ASKorinskiy](https://www.ruby-forum.com/u/Kirill_ASKorinskiy)\
**Post date:** [March 19, 2009, 1:48am UTC](https://www.ruby-forum.com/t/start-use-capabilities-on-linux/163170/5 "2009-03-19T01:48:53Z")

</div>

From: Kirill A. Korinskiy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

* * *

src/os/unix/ngx\_user.c | 1 +  
1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/src/os/unix/ngx\_user.c b/src/os/unix/ngx\_user.c  
index  
d3f6b5bd1d7d379ff4de9418955bcc285f0696d4…c6f0bdb7a16ef5d896998f56bfd9532ff6689dad  
100644  
— a/src/os/unix/ngx\_user.c  
+++ b/src/os/unix/ngx\_user.c  
@@ -8,6 +8,7 @@  
#include \<ngx\_core.h\>

#if (NGX\_HAVE\_CAPABILITIES)  
+#undef \_POSIX\_SOURCE  
#include \<sys/capability.h\>  
#endif

---

<div class="post-metadata">

**Author:** ![Kirill\_ASKorinskiy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e274bd/32.png) [@Kirill\_ASKorinskiy](https://www.ruby-forum.com/u/Kirill_ASKorinskiy)\
**Post date:** [March 18, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/start-use-capabilities-on-linux/163170/6 "2009-03-18T17:26:12Z")

</div>

From: Kirill A. Korinskiy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

capset(CAP\_CHOWN) grand the proccess rule to change arbitrary to file.

CAP\_DAC\_OVERRIDE and CAP\_DAC\_READ\_SEARCH bypass file read, write and  
execute permission checks.

## CAP\_SETUID need for rewert back to privileged processes (whose effective user ID is 0).

src/os/unix/ngx\_process.c | 9 +++++++++  
src/os/unix/ngx\_user.c | 13 ++++++++++±-  
2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/os/unix/ngx\_process.c b/src/os/unix/ngx\_process.c  
index  
18b2601aff17895cc250e54621651d72aee09790..9f06fdfa460ceadac032a62aae18559238fc17c6  
100644  
— a/src/os/unix/ngx\_process.c  
+++ b/src/os/unix/ngx\_process.c  
@@ -247,6 +247,15 @@ ngx\_execute\_proc(ngx\_cycle\_t \*cycle, void \*data)  
{  
ngx\_exec\_ctx\_t \*ctx = data;

+#if (NGX\_HAVE\_CAPABILITIES)

- if (setuid(0) == -1) {

- 

```
   ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                 "setuid(0) failed");

```

- 

```
   /* fatal */

```

- 

```
   exit(2);

```

- }  
+#endif

- if (execve(ctx-\>path, ctx-\>argv, ctx-\>envp) == -1) {  
ngx\_log\_error(NGX\_LOG\_ALERT, cycle-\>log, ngx\_errno,  
“execve() failed while executing %s "%s"”,  
diff --git a/src/os/unix/ngx\_user.c b/src/os/unix/ngx\_user.c  
index  
d5ebb6c3e22b96d66a5e816dbfa860635fa7a3d8..d3f6b5bd1d7d379ff4de9418955bcc285f0696d4  
100644  
— a/src/os/unix/ngx\_user.c  
+++ b/src/os/unix/ngx\_user.c  
@@ -149,14 +149,23 @@ void ngx\_set\_capabilities(ngx\_cycle\_t \*cycle)

- prctl(PR\_SET\_KEEPCAPS, 1, 0, 0, 0);

- if (prctl(PR\_SET\_KEEPCAPS, 1, 0, 0, 0) == -1) {

- 

```
   ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,

```

- 

```
                 "prctl(%d) failed", ccf->user);

```

- 

```
   /* fatal */

```

- 

```
   exit(2);

```

- }

- cap.effective = cap.permitted = (1 \<\< CAP\_NET\_BIND\_SERVICE);

- cap.effective = cap.permitted = (1 \<\< CAP\_NET\_BIND\_SERVICE |

- 

```
        1 << CAP_CHOWN |

```

- 

```
        1 << CAP_DAC_OVERRIDE |

```

- 

```
        1 << CAP_DAC_READ_SEARCH |

```

- 

```
        1 << CAP_SETUID);

```

if (capset(&header, &cap) == -1) {  
ngx\_log\_error(NGX\_LOG\_EMERG, cycle-\>log, ngx\_errno,
