diff --git a/README.markdown b/README.markdown index 7a29d09f6b..171a1e24ea 100644 --- a/README.markdown +++ b/README.markdown @@ -3731,6 +3731,8 @@ lua_ssl_key_log Enables logging of client connection SSL keys in the [tcpsock:sslhandshake](#tcpsocksslhandshake) method and specifies the path to the key log file. Keys are logged in the SSLKEYLOGFILE format compatible with Wireshark. +The key log contains TLS session secrets and must be protected accordingly. On Unix, a new key log is created with owner-only permissions. An existing key log must be owned by the user that starts the nginx master process and must not grant permissions to group or other users; otherwise, nginx rejects the configuration. Place the key log in a directory that untrusted local users cannot modify. + [Back to TOC](#directives) lua_ssl_conf_command diff --git a/doc/HttpLuaModule.wiki b/doc/HttpLuaModule.wiki index 979ed9841b..dbc50f6a6d 100644 --- a/doc/HttpLuaModule.wiki +++ b/doc/HttpLuaModule.wiki @@ -2990,6 +2990,8 @@ See also [[#lua_ssl_certificate|lua_ssl_certificate]], [[#lua_ssl_certificate_ke Enables logging of client connection SSL keys in the [[#tcpsock:sslhandshake|tcpsock:sslhandshake]] method and specifies the path to the key log file. Keys are logged in the SSLKEYLOGFILE format compatible with Wireshark. +The key log contains TLS session secrets and must be protected accordingly. On Unix, a new key log is created with owner-only permissions. An existing key log must be owned by the user that starts the nginx master process and must not grant permissions to group or other users; otherwise, nginx rejects the configuration. Place the key log in a directory that untrusted local users cannot modify. + == lua_ssl_conf_command == '''syntax:''' ''lua_ssl_conf_command '' diff --git a/src/ngx_http_lua_module.c b/src/ngx_http_lua_module.c index 56ca2ec0ca..b0c55d5e75 100644 --- a/src/ngx_http_lua_module.c +++ b/src/ngx_http_lua_module.c @@ -1951,6 +1951,9 @@ static ngx_int_t ngx_http_lua_ssl_key_log(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *file) { ngx_fd_t fd; +#if !(NGX_WIN32) + ngx_file_info_t fi; +#endif ngx_http_lua_ssl_key_log_t *ssl_key_log; ngx_pool_cleanup_t *cln; @@ -1970,13 +1973,35 @@ ngx_http_lua_ssl_key_log(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *file) * append so that existing keylog file contents can be preserved */ fd = ngx_open_file(file->data, NGX_FILE_APPEND, NGX_FILE_CREATE_OR_OPEN, - NGX_FILE_DEFAULT_ACCESS); + NGX_FILE_OWNER_ACCESS); if (fd == NGX_INVALID_FILE) { ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, ngx_open_file_n "(\"%V\") failed", file); return NGX_ERROR; } +#if !(NGX_WIN32) + if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { + ngx_log_error(NGX_LOG_EMERG, ssl->log, ngx_errno, + ngx_fd_info_n "(\"%V\") failed", file); + goto failed; + } + + if (fi.st_uid != geteuid()) { + ngx_log_error(NGX_LOG_EMERG, ssl->log, 0, + "the SSL key log file \"%V\" must be owned by the " + "nginx master process user", file); + goto failed; + } + + if (ngx_file_access(&fi) & ~NGX_FILE_OWNER_ACCESS) { + ngx_log_error(NGX_LOG_EMERG, ssl->log, 0, + "the SSL key log file \"%V\" must have owner-only " + "permissions", file); + goto failed; + } +#endif + ssl_key_log = ngx_palloc(cf->pool, sizeof(ngx_http_lua_ssl_key_log_t)); if (ssl_key_log == NULL) { ngx_ssl_error(NGX_LOG_EMERG, ssl->log, 0, "ngx_pcalloc() failed"); @@ -2007,6 +2032,17 @@ ngx_http_lua_ssl_key_log(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *file) SSL_CTX_set_keylog_callback(ssl->ctx, key_log_callback); return NGX_OK; + +#if !(NGX_WIN32) +failed: + + if (ngx_close_file(fd) == NGX_FILE_ERROR) { + ngx_log_error(NGX_LOG_ALERT, ssl->log, ngx_errno, + ngx_close_file_n "(\"%V\") failed", file); + } + + return NGX_ERROR; +#endif } #endif diff --git a/t/200-ssl-key-log-permissions.t b/t/200-ssl-key-log-permissions.t new file mode 100644 index 0000000000..ef0d3203c0 --- /dev/null +++ b/t/200-ssl-key-log-permissions.t @@ -0,0 +1,55 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: + +use Test::Nginx::Socket::Lua 'no_plan'; + +repeat_each(1); +no_shuffle(); + +my $old_umask = umask 0022; +add_cleanup_handler(sub { umask $old_umask; }); + +run_tests(); + +__DATA__ + +=== TEST 1: new SSL key log is owner-only +--- skip_openssl: 4: < 1.1.1 +--- config + lua_ssl_key_log sslkey.log; + + location /t { + return 200 "ok\n"; + } +--- init +my $key_log = server_root() . "/conf/sslkey.log"; +my $mode = (stat $key_log)[2]; +defined $mode or die "failed to stat $key_log: $!"; +$mode &= 0777; +die sprintf("unexpected key log mode %04o", $mode) unless $mode == 0600; +--- request +GET /t +--- response_body +ok +--- no_error_log +[error] +[alert] +[emerg] + + + +=== TEST 2: broad existing SSL key log permissions are rejected +--- skip_openssl: 2: < 1.1.1 +--- config + lua_ssl_key_log ../html/sslkey.log; + + location /t { + return 200; + } +--- user_files +>>> sslkey.log +existing key log +--- request +GET /t +--- must_die +--- error_log eval +qr/\[emerg\].*the SSL key log file ".*sslkey\.log" must have owner-only permissions/