-
Notifications
You must be signed in to change notification settings - Fork 367
Added (fixed) support for abstract Unix sockets. #740
Conversation
|
One detail: Only the first If the user specifies |
|
What I've already tested is configuring the following: {
"listeners": {
"unix:@magic": {
"pass": "routes",
"client_ip": {
"header": "X-Forwarded-For",
"source": "unix"
}
}
},
"routes": [{
"action": {
"share": "/home/alx/srv/www/$uri"
}
}]
}And then: And some sanity check: And also, I tested a simple C program to connect to the socket and write to it some random stuff, and it (kind of) worked: the socket name has a trailing |
|
I'm trying to test this with curl, for which I need a socket name that doesn't end in So, I have the following debug changes, and even though I'm setting the length to 3 (and Still, I see 9 (2 + 7) bytes in the log: And ss(8) still sees also |
|
Just take a glance.
|
Yeah, it seems it wasn't entering my conditional. Heh! Well, then, we have still some issue. There's a trailing |
Although, that code doesn't work. The listener doesn't support abstract sockets previous to my patch. What was that code used for? Or was it dead code? |
|
It's for disabling |
I can't get the code inside the if (sa->u.sockaddr_un.sun_path[0] == '@') { to trigger Doing sa->u.sockaddr_un.sun_path[0] = '\0';
sa->socklen--;before that if check makes things work. Although I think we may have tickled a bug somewhere as now ss only shows @magic as magic, even though the bind(2) is doing and it shows up correctly in /proc/net/unix but But at least it works |
s/behavior/behaviour/
s/alive/to live/
s/bug/error/
s/be more/be no more/ You can also use SO_PEERCRED on Unix Domain Sockets.
s/an/a/ |
Unfortunately, we default to US English :( |
That's very weird. A bug in ss(8)? Could you please try my Another test could be to try to run I don't trust curl(1) maybe doing weird stuff. It's too complex of a program. |
|
It's a bug, in xterm! I've seen this before, and it just occurred to me again now. Lets see xterm at 80 chars wide. The 'm' is at the 80 char mark. So we got magic even though there is apparently no '@'! Now lets extend xterm out a little (even just by 1 char). The '@' is now at the 80 char mark. There we go. Previously I'd already had xterm wider for the ss output so didn't notice this then. I've noticed this problem in xterm before, particularly with gcc output. |
This code doesn't seem to be executed, at least not before we call bind(2).
Yeah, that's required. Here's a minimal patch on top of master to make it work. diff --git a/src/nxt_main_process.c b/src/nxt_main_process.c
index 03761a10..a8de8f89 100644
--- a/src/nxt_main_process.c
+++ b/src/nxt_main_process.c
@@ -1143,6 +1143,12 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls)
#endif
+ if (sa->u.sockaddr.sa_family == AF_UNIX
+ && *sa->u.sockaddr_un.sun_path == '\0')
+ {
+ sa->socklen--;
+ }
+
if (bind(s, &sa->u.sockaddr, sa->socklen) != 0) {
err = nxt_errno;
@@ -1187,7 +1193,9 @@ nxt_main_listening_socket(nxt_sockaddr_t *sa, nxt_listening_socket_t *ls)
#if (NXT_HAVE_UNIX_DOMAIN)
- if (sa->u.sockaddr.sa_family == AF_UNIX) {
+ if (sa->u.sockaddr.sa_family == AF_UNIX
+ && *sa->u.sockaddr_un.sun_path != '\0')
+ {
char *filename;
mode_t access;NOTE: I'm not saying this is correct, but it shows the code as is, is not far off (though the socket name does show up in the config as \u0000magic) |
Correct, the related APIs have not been used yet. |
|
Digging, I found a lot of dead code that was accidentally left in some of Igor's refactors. I removed it all. |
|
I found more or less why we are getting the extra length. See the following debug log: Got that with the following debug code: diff --git a/src/nxt_sockaddr.c b/src/nxt_sockaddr.c
index 8220590e..75c1efde 100644
--- a/src/nxt_sockaddr.c
+++ b/src/nxt_sockaddr.c
@@ -141,6 +141,10 @@ nxt_sockaddr_create(nxt_mp_t *mp, struct sockaddr *sockaddr, socklen_t length,
#endif
}
+fprintf(stderr, "ALX: %s():%i: %i\n ALX: ", __func__, __LINE__, (int) size-2);
+for (int i = 0; i < 6; i++)
+fprintf(stderr, "<%c>", sockaddr->sa_data[i]);
+fprintf(stderr, "\n");
#endif /* NXT_HAVE_UNIX_DOMAIN */
@@ -286,6 +290,10 @@ nxt_sockaddr_text(nxt_sockaddr_t *sa)
#if (NXT_LINUX)
+fprintf(stderr, "ALX: %s():%i: %i\n ALX: ", __func__, __LINE__, (int) sa->socklen - 2);
+for (int i = 0; i < 6; i++)
+fprintf(stderr, "<%c>", p[i]);
+fprintf(stderr, "\n");
if (p[0] == '\0') {
size_t length;
@@ -294,8 +302,16 @@ nxt_sockaddr_text(nxt_sockaddr_t *sa)
p = nxt_sprintf(start, end, "unix:@%*s", length - 1, p + 1);
+fprintf(stderr, "ALX: %s():%i: %i\n ALX: ", __func__, __LINE__, (int) length);
+for (int i = 0; i < 6; i++)
+fprintf(stderr, "<%c>", start[i]);
+fprintf(stderr, "\n");
} else {
p = nxt_sprintf(start, end, "unix:%s", p);
+fprintf(stderr, "ALX: %s():%i:\n ALX: ", __func__, __LINE__);
+for (int i = 0; i < 6; i++)
+fprintf(stderr, "<%c>", start[i]);
+fprintf(stderr, "\n");
}
#else /* !(NXT_LINUX) */
@@ -612,10 +628,18 @@ nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr)
* are covered by the specified length of the address structure.
* (Null bytes in the name have no special significance.)
*/
+fprintf(stderr, "ALX: %s():%i: %i\n ALX: ", __func__, __LINE__, (int) socklen - 2);
+for (int i = 0; i < 6; i++)
+fprintf(stderr, "<%c>", path[i]);
+fprintf(stderr, "\n");
if (path[0] == '@') {
path[0] = '\0';
socklen--;
}
+fprintf(stderr, "ALX: %s():%i: %i\n ALX: ", __func__, __LINE__, (int) socklen - 2);
+for (int i = 0; i < 6; i++)
+fprintf(stderr, "<%c>", path[i]);
+fprintf(stderr, "\n");
#endif
The explanation is that we're going twice through The easy fix: if (path[0] == '@') {
path[0] = '\0';
socklen--;
}should be transformed into if (path[0] == '@') {
path[0] = '\0';
}
if (path[0] == '\0') {
socklen--;
}But, as I hinted, the fix should probably be to make sure that we only run through that function once. I configured with the following config file: $ cat sock_static.json
{
"listeners": {
"unix:@magic": {
"pass": "routes",
"client_ip": {
"header": "X-Forwarded-For",
"source": "unix"
}
}
},
"routes": [{
"action": {
"share": "/home/alx/srv/www/$uri"
}
}]
} |
Wontfix :)
Fixed. Thanks! |
|
I found the following: with the following debug code: So, we first parse the string when we validate it, and then again in the router when we use it. Knowing this, I don't think it can hurt to do the simple patch I suggested yesterday. I'm worried that some path of the existing code might not be prepared to handle a '\0' in the middle of the socket name. However, since that code already existed (i.e., we wouldn't be introducing new bugs or vulnerabilities), I think it's simpler to leave it untouched (assume it still works), and only change it if we find a bug, rather than trying to make sure that it's correct (which would be much harder). As long as my tests don't detect anything weird, I'm happy with it. What do you think about it? |
|
A general inspection of the code looks good to me. In the conf validation, we parse the socket address to check it's a valid one, and discard the result. Later in the router, we parse it again to use it. Nothing very weird. We could maybe keep the parsed data to not have to parse it twice... but that would require work that may be unnecessary, and is not vital, I think; there are no obvious places that would be unreasonably slow in that code. |
|
Tested: |
|
This code should work for you, I guess. Now I'll try to make NGINX work with abstract sockets too. |
src/nxt_sockaddr.c
Outdated
| */ | ||
| if (path[0] == '@') { | ||
| switch (path[0]) { | ||
| case '@': | ||
| path[0] = '\0'; | ||
| /* fall through */ | ||
| case '\0': | ||
| socklen--; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of switch/case.
The only thing I'd point out is that the socket name is stored in the config as 'unix:\u0000magic'. Not saying that's a problem, just pointing it out...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of switch/case.
It looks soo nice! =)
The only thing I'd point out is that the socket name is stored in the config as 'unix:\u0000magic'. Not saying that's a problem, just pointing it out...
I checked that JSON allows that: https://www.rfc-editor.org/rfc/rfc7159#section-7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The commit title is Fixed support for abstract Unix sockets., but the changelog is supporting abstract UNIX sockets..
Is it ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first commit just fixes the small bugs in the old code that already intended to support abstract sockets. That's why the commit message says "Fixed support ...". However, since we have never officially supported abstract sockets, I used "supporting ..." in the changelog.
ac000
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is certainly the simplest and safest fix and if you wanted to have it stored in the config as @name than that could be done separately as it's likely to be more intrusive, so I'd say this was good to go.
Yep, I have no preference between |
Interesting, so it's not actually a lot of code, but at the same time, I just don't see the need... |
Yeah, I expected it to be a diff larger than the implementation, but it seems smaller. Still, it's comparable to the whole implementation diff, so I'd say it's big. :) The two patches were (ignoring changelog): |
I'm still confused about the feature. If we have supported the feature of abstract sockets by the first commit, why still need the second commit? The second one seems like a fixing. Since storing |
The first commit simply makes abstract sockets work, you can specify '\u0000' or '@' in the config file and both work.
However with just the first commit, if a user puts '@' in the config then when they read the config back from unit, they get '\u0000' instead. This is what the second commit changes, they can still use '\u0000' or '@', but when they read back the config it will show them whatever they used. So if they put '@', then it will show back as '@' (rather than '\u0000'). |
That’s my question, if putting ‘@‘ in the configuration, they should get the same ‘@‘, right? |
Yes. See the test here: |
After the second commit, yes. The first commit simply fixes the existing code to actually make abstract sockets work. |
The first commit just fixes the small bugs in the old code that already intended to support abstract sockets. That's why the commit message says "Fixing ...". However, since we have never officially supported abstract sockets, I used "Added support ..." in the changes file.
It's not required. Abstract sockets would work with both The second commit changes the way we store the socket name in the internal state file (which is observable by a user that queries the control socket). But it doesn't affect in any way the creation of the abstract socket. |
|
I think we should also add the following code to the first commit: diff --git a/src/nxt_sockaddr.c b/src/nxt_sockaddr.c
index 63d3ffb3..a90bb392 100644
--- a/src/nxt_sockaddr.c
+++ b/src/nxt_sockaddr.c
@@ -601,8 +601,6 @@ nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr)
socklen = offsetof(struct sockaddr_un, sun_path) + length + 1;
-#if (NXT_LINUX)
-
/*
* Linux unix(7):
*
@@ -618,9 +616,12 @@ nxt_sockaddr_unix_parse(nxt_mp_t *mp, nxt_str_t *addr)
/* fall through */
case '\0':
socklen--;
- }
-
+#if !(NXT_LINUX)
+ nxt_thread_log_error(NXT_LOG_ERR,
+ "abstract unix domain sockets are not supported");
+ return NULL;
#endif
+ }
sa = nxt_sockaddr_alloc(mp, socklen, addr->length);
What do you think? Otherwise, we would probably be silently creating files starting with |
|
My 2 cents:
|
Sure, we can. In fact, my suggestion for the changelog is just
For me, that would be fine. I'd be happy accepting abstract sockets exactly as the kernel wants them (that is @lcrilly suggested using the So that's why my patch set supports both syntaxes. |
|
I wonder what happens if you pass |
|
Just to confirm again before reviewing.
|
Yes.
I prefer two commits. BTW, all of the commits are on RB too, if you want to review there. |
|
I checked Envoy's source code, and I predict that they reject PipeInstance::PipeInstance(const std::string& pipe_path, mode_t mode,
const SocketInterface* sock_interface)
: InstanceBase(Type::Pipe, sockInterfaceOrDefault(sock_interface)) {
if (pipe_path.size() >= sizeof(pipe_.address_.sun_path)) {
throw EnvoyException(
fmt::format("Path \"{}\" exceeds maximum UNIX domain socket path size of {}.", pipe_path,
sizeof(pipe_.address_.sun_path)));
}
memset(&pipe_.address_, 0, sizeof(pipe_.address_));
pipe_.address_.sun_family = AF_UNIX;
if (pipe_path[0] == '@') {
// This indicates an abstract namespace.
// In this case, null bytes in the name have no special significance, and so we copy all
// characters of pipe_path to sun_path, including null bytes in the name. The pathname must also
// be null terminated. The friendly name is the address path with embedded nulls replaced with
// '@' for consistency with the first character.
#if !defined(__linux__)
throw EnvoyException("Abstract AF_UNIX sockets are only supported on linux.");
#endif
if (mode != 0) {
throw EnvoyException("Cannot set mode for Abstract AF_UNIX sockets");
}
pipe_.abstract_namespace_ = true;
pipe_.address_length_ = pipe_path.size();
// The following statement is safe since pipe_path size was checked at the beginning of this
// function
memcpy(&pipe_.address_.sun_path[0], pipe_path.data(), pipe_path.size()); // NOLINT(safe-memcpy)
pipe_.address_.sun_path[0] = '\0';
pipe_.address_.sun_path[pipe_path.size()] = '\0';
friendly_name_ = friendlyNameFromAbstractPath(
absl::string_view(pipe_.address_.sun_path, pipe_.address_length_));
} else {
// Throw an error if the pipe path has an embedded null character.
if (pipe_path.size() != strlen(pipe_path.c_str())) {
throw EnvoyException("UNIX domain socket pathname contains embedded null characters");
}
StringUtil::strlcpy(&pipe_.address_.sun_path[0], pipe_path.c_str(),
sizeof(pipe_.address_.sun_path));
friendly_name_ = pipe_.address_.sun_path;
}
pipe_.mode_ = mode;
} |
| #if !(NXT_LINUX) | ||
| nxt_thread_log_error(NXT_LOG_ERR, | ||
| "abstract unix domain socket \"%V\" is not supported", | ||
| addr); | ||
| return NULL; | ||
| #endif | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so this enables improved error messages in !Linux, e.g testing on FreeBSD, using either '@magic' or '\u0000magic' for the socket name would result in
{
"error": "Failed to apply new configuration."
}Now we get the likes of
{
"error": "Invalid configuration.",
"detail": "The listener address \"unix:@magic\" is invalid."
}So LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, did FreeBSD result in an error before this addition? I was worried that without this, FreeBSD might try to create a file with a leading @ or \0. If it's not the case, I prefer not having this extra code. Especially since using abstract sockets in FreeBSD is nonsense, so I don't want to add much code to make it nicer. I just want to make sure it fails hard, instead of doing weird stuff silently
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could/did you try?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, scrub that. If I revert that error check
- It will create a socket called '@magic' in the processes current working directory (if it can).
- It does error out with "Failed to apply new configuration." with '\u0000magic'.
|
Tested with the following: alx@asus5775:~/etc/unitd$ cat abs_sock.json
{
"listeners": {
"unix:@unitd.static.sock": {
"pass": "routes",
"client_ip": {
"header": "X-Forwarded-For",
"source": "unix"
}
},
"unix:@unitd.perl.sock": {
"pass": "applications/perl",
"client_ip": {
"header": "X-Forwarded-For",
"source": "unix"
}
},
"unix:@unitd.php.sock": {
"pass": "applications/php",
"client_ip": {
"header": "X-Forwarded-For",
"source": "unix"
}
},
"unix:@unitd.asgi.sock": {
"pass": "applications/asgi",
"client_ip": {
"header": "X-Forwarded-For",
"source": "unix"
}
},
"unix:@unitd.python.sock": {
"pass": "applications/python",
"client_ip": {
"header": "X-Forwarded-For",
"source": "unix"
}
},
"unix:@unitd.ruby.sock": {
"pass": "applications/ruby",
"client_ip": {
"header": "X-Forwarded-For",
"source": "unix"
}
}
},
"applications": {
"perl": {
"type": "perl",
"working_directory": "/home/alx/srv/www/perl/",
"script": "./sock.psgi"
},
"php": {
"type": "php",
"root": "/home/alx/srv/www/php/",
"index": "sock.php"
},
"asgi": {
"type": "python",
"path": "/home/alx/srv/www/asgi/",
"module": "sock"
},
"python": {
"type": "python",
"path": "/home/alx/srv/www/python/",
"module": "sock"
},
"ruby": {
"type": "ruby",
"working_directory": "/home/alx/srv/www/ruby",
"script": "sock.ru"
}
},
"routes": [{
"action": {
"share": "/home/alx/srv/www/$uri"
}
}]
}alx@asus5775:~/etc/unitd$ sudo curl -X PUT -d @abs_sock.json --unix-sock /opt/local/unit/control.unit.sock localhost/config
{
"success": "Reconfiguration done."
}$ ss -l | grep @unitd
u_str LISTEN 0 511 @unitd.php.sock 523347 * 0
u_str LISTEN 0 511 @unitd.perl.sock 523346 * 0
u_str LISTEN 0 511 @unitd.python.sock 523348 * 0
u_str LISTEN 0 511 @unitd.static.sock 523345 * 0
u_str LISTEN 0 511 @unitd.asgi.sock 524535 * 0
u_str LISTEN 0 511 @unitd.ruby.sock 523349 * 0 $ curl --abstract-unix-sock unitd.python.sock localhost
Hello, Python on Unit!
$ curl --abstract-unix-sock unitd.asgi.sock localhost
Hello, ASGI
$ curl --abstract-unix-sock unitd.static.sock localhost
idx
$ curl --abstract-unix-sock unitd.perl.sock localhost
Hello, Perl on Unit!
$ curl --abstract-unix-sock unitd.php.sock localhost
Hello, PHP on Unit!(I cheated a bit and added newlines to the output above, since some apps weren't producing a trailing newline, but that's fair.) |
|
Just to confirm that with "Removed trailing \0 in abstract sockets." split out from "Fixed support for abstract Unix sockets." that if you specify a socket as If so, then perhaps that commit should come immediately after the first one, otherwise there's a small window where abstract sockets can work but need to be specified with a trailing \0' to where they are just specified as normal. Was just thinking if this could potentially cause an issue with git-bisect(1) where you could end up in the middle of this patch set?... if you're debugging an issue (any issue) between the version that fixed abstract sockets and say the following version and you're actually using abstract sockets... |
That is fixed now (indirectly) by 739cef5 "Storing abstract sockets with @ internally.". |
I wouldn't mind merging both patches. But currently, the fix is consecutive (any of the 2nd and 4th patches do fix that, in different ways), so I guess a bisect would be fine too. |
Yes, both manual testing and pytests pass with just the first two commits. |
|
Great. Although in FreeBSD pytests fail, because they try to use |
Unix domain sockets are normally backed by files in the
filesystem. This has historically been problematic when closing
and opening again such sockets, since SO_REUSEADDR is ignored for
Unix sockets (POSIX left the behavior of SO_REUSEADDR as
implementation-defined, and most --if not all-- implementations
decided to just ignore this flag).
Many solutions are available for this problem, but all of them
have important caveats:
- unlink(2) the file when it's not needed anymore.
This is not easy, because the process that controls the fd may
not be the same process that created the file, and may not have
file permissions to remove it.
Further solutions can be applied to that caveat:
- unlink(2) the file right after creation.
This will remove the pathname from the filesystem without
closing the socket (it will continue to live until the last fd
is closed). This is not useful for us, since we need the
pathname of the socket as its interface.
- chown(2) or chmod(2) the directory that contains the socket.
For removing a file from the filesystem, a process needs
write permissions in the containing directory. We could
put sockets in dummy directories that can be chown(2)ed to
nobody. This could be dangerous, though, as we don't control
the socket names. It is our users who configure the socket
name in their configuration, and so it's easy that they don't
understand the many implications of not chosing an appropriate
socket pathname. A user could unknowingly put the socket in a
directory that is not supposed to be owned by user nobody, and
if we blindly chown(2) or chmod(2) the directory, we could be
creating a big security hole.
- Ask the main process to remove the socket.
This would require a very complex communication mechanism with
the main process, which is not impossible, but let's avoid it
if there are simpler solutions.
- Give the child process the CAP_DAC_OVERRIDE capability.
That is one of the most powerful capabilities. A process with
that capability can be considered root for most practical
aspects. Even if the capability is disabled for most of the
lifetime of the process, there's a slight chance that a
malicious actor could activate it and then easily do serious
damage to the system.
- unlink(2) the file right before calling bind(2).
This is dangerous because another process (for example, another
running instance of unitd(8)), could be using the socket, and
removing the pathname from the filesystem would be problematic.
To do this correctly, a lot of checks should be added before the
actual unlink(2), which is error-prone, and difficult to do
correctly, and atomically.
- Use abstract-namespace Unix domain sockets.
This is the simplest solution, as it only requires accepting a
slightly different syntax (basically a @ prefix) for the socket
name, to transform it into a string starting with a null byte
('\0') that the kernel can understand. The patch is minimal.
Since abstract sockets live in an abstract namespace, they don't
create files in the filesystem, so there's no need to remove
them later. The kernel removes the name when the last fd to it
has been closed.
One caveat is that only Linux currently supports this kind of
Unix sockets. Of course, a solution to that could be to ask
other kernels to implement such a feature.
Another caveat is that filesystem permissions can't be used to
control access to the socket file (since, of course, there's no
file). Anyone knowing the socket name can access to it. The
only method to control access to it is by using
network_namespaces(7). Since in unitd(8) we're using 0666 file
sockets, abstract sockets should be no more insecure than that
(anyone can already read/write to the listener sockets).
- Ask the kernel to implement a simpler way to unlink(2) socket
files when they are not needed anymore. I've suggested that to
the <linux-fsdevel@vger.kernel.org> mailing list, in:
<lore.kernel.org/linux-fsdevel/0bc5f919-bcfd-8fd0-a16b-9f060088158a@gmail.com/T>
In this commit, I decided to go for the easiest/simplest solution,
which is abstract sockets. In fact, we already had partial
support. This commit only fixes some small bug in the existing
code so that abstract Unix sockets work:
- Don't chmod(2) the socket if it's an abstract one.
This fixes the creation of abstract sockets, but doesn't make them
usable, since we produce them with a trailing '\0' in their name.
That will be fixed in the following commit.
This closes #669 issue on GitHub.
We accept both "\u0000socket-name" and "@socket-name" as abstract unix sockets. The first one is passed to the kernel pristine, while the second is transformed '@'->'\0'. The commit that added support for unix sockets accepts both variants, but we internally stored it in the same way, using "\u0000..." for both. We want to support abstract sockets transparently to the user, so that if the user configures unitd with '@', if we receive a query about the current configuration, the user should see the same exact thing that was configured. So, this commit avoids the transformation in the internal state file, storing user input pristine, and we only transform the '@' for a string that will be used internally (not user-visible). This commit (indirectly) fixes a small bug, where we created abstract sockets with a trailing '\0' in their name due to calling twice nxt_sockaddr_parse() on the same string. By calling that function only once with each copy of the string, we have fixed that bug.
The previous commit added/fixed support for abstract Unix domain sockets on Linux with a leading '@' or '\0'. To be consistent in all platforms, treat those prefixes as markers for abstract sockets in all platforms, and fail if abstract sockets are not supported by the platform. That will avoid mistakes when copying a config file from a Linux system and using it in non-Linux, which would surprisingly create a normal socket.
Sockets starting with '\0' had a trailing '\0' character. That made it hard to use tools like curl(1) with them.
|
Merged (except for |
Unix domain sockets are normally backed by files in the
filesystem. This has historically been problematic when closing
and opening again such sockets, since SO_REUSEADDR is ignored for
Unix sockets (POSIX left the behavior of SO_REUSEADDR as
implementation-defined, and most --if not all-- implementations
decided to just ignore this flag).
Many solutions are available for this problem, but all of them
have important caveats:
unlink(2) the file when it's not needed anymore.
This is not easy, because the process that controls the fd may
not be the same process that created the file, and may not have
file permissions to remove it.
Further solutions can be applied to that caveat:
unlink(2) the file right after creation.
This will remove the pathname from the filesystem without
closing the socket (it will continue alive until the last fd
is closed). This is not useful for us, since we need the
pathname of the socket as its interface.
chown(2) or chmod(2) the directory that contains the socket.
For removing a file from the filesystem, a process needs
write permissions in the containing directory. We could
put sockets in dummy directories that can be chown(2)ed to
nobody. This could be dangerous, though, as we don't control
the socket names. It is our users who configure the socket
name in their configuration, and so it's easy that they don't
understand the many implications of not chosing an appropriate
socket pathname. A user could unknowingly put the socket in a
directory that is not supposed to be owned by user nobody, and
if we blindly chown(2) or chmod(2) the directory, we could be
creating a big security hole.
Ask the main process to remove the socket.
This would require a very complex communication mechanism with
the main process, which is not impossible, but let's avoid it
if there are simpler solutions.
Give the child process the CAP_DAC_OVERRIDE capability.
That is one of the most powerful capabilities. A process with
that capability can be considered root for most practical
aspects. Even if the capability is disabled for most of the
lifetime of the process, there's a slight chance that a
malicious actor could activate it and then easily do serious
damage to the system.
unlink(2) the file right before calling bind(2).
This is dangerous because another process (for example, another
running instance of unitd(8)), could be using the socket, and
removing the pathname from the filesystem would be problematic.
To do this correctly, a lot of checks should be added before the
actual unlink(2), which is bug-prone, and difficult to do
correctly, and atomically.
Use abstract-namespace Unix domain sockets.
This is the simplest solution, as it only requires accepting a
slightly different syntax (basically a @ prefix) for the socket
name, to transform it into a string starting with a null byte
('\0') that the kernel can understand. The patch is minimal.
Since abstract sockets live in an abstract namespace, they don't
create files in the filesystem, so there's no need to remove
them later. The kernel removes the name when the last fd to it
has been closed.
One caveat is that only Linux currently supports this kind of
Unix sockets. Of course, a solution to that could be to ask
other kernels to implement such a feature.
Another caveat is that filesystem permissions can't be used to
control access to the socket file (since, of course, there's no
file). Anyone knowing the socket name can access to it. The
only method to control access to it is by using
network_namespaces(7). Since in unitd(8) we're using 0666 file
sockets, abstract sockets should be more insecure than that
(anyone can already read/write to the listener sockets).
Ask the kernel to implement an simpler way to unlink(2) socket
files when they are not needed anymore. I've suggested that to
the linux-fsdevel@vger.kernel.org mailing list, in:
<lore.kernel.org/linux-fsdevel/0bc5f919-bcfd-8fd0-a16b-9f060088158a@gmail.com/T>
In this commit, I decided to go for the easiest/simplest solution.
This closes #669 issue on GitHub.