From 83ccb1c76b2b88f62bfba2c58e2edd419b089756 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Mon, 23 Nov 2015 16:26:42 -0800 Subject: [PATCH] init: Don't ignore setsockcreatecon errors The init language supports setting the creation context of a socket as the 6th argument to the socket keyword. For example, in the following service, the context associated with the netd socket is u:r:netd:s0 service netd /system/bin/netd class main socket netd stream 0660 root system u:r:netd:s0 socket dnsproxyd stream 0660 root inet socket mdns stream 0660 root system socket fwmarkd stream 0660 root inet The 6 argument form of the socket statement is rarely if ever used, since the init code supplies a sensible default. Currently, there's no error checking on the value supplied as the 6th argument. For example, if you have the following socket statement: socket netd stream 0660 root system graphics a socket will attempt to get created with an invalid "graphics" context. When setsockcreatecon fails, it retains the default socket creation context, which for init is u:r:init:s0. This results in a socket being created which is in an unexpected context. Check the return value from the setsockcreatecon() call. If an invalid context is specified, return early and don't subsequently attempt to create the socket with the default context. Bug: 25851205 Change-Id: Ic66cd6f7efe3897fb247b587ddeac5d35e1602b7 --- init/util.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/init/util.cpp b/init/util.cpp index b316d6ec4..c7d031404 100644 --- a/init/util.cpp +++ b/init/util.cpp @@ -105,8 +105,12 @@ int create_socket(const char *name, int type, mode_t perm, uid_t uid, int fd, ret; char *filecon; - if (socketcon) - setsockcreatecon(socketcon); + if (socketcon) { + if (setsockcreatecon(socketcon) == -1) { + ERROR("setsockcreatecon(\"%s\") failed: %s\n", socketcon, strerror(errno)); + return -1; + } + } fd = socket(PF_UNIX, type, 0); if (fd < 0) {