Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fa2f985b authored by Ashish Sharma's avatar Ashish Sharma Committed by JP Abgrall
Browse files

libcutils: qtaguid: support socket untagging, return errors.



  - Enable and rename qtaguid_tagSocket()
  - Add qtaguid_untagSocket()
  - Return kernel errors to caller

Change-Id: I8e33c8832b7f6b24ed9081f36ce1ea9ae6b099c0
Signed-off-by: default avatarAshish Sharma <ashishsharma@google.com>
parent 86993946
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -28,7 +28,12 @@ extern "C" {
/*
 * Set tags (and owning UIDs) for network sockets.
*/
extern int set_qtaguid(int sockfd, int tag, uid_t uid);
extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid);

/*
 * Untag a network socket before closing.
*/
extern int qtaguid_untagSocket(int sockfd);

#ifdef __cplusplus
}
+36 −13
Original line number Diff line number Diff line
@@ -19,26 +19,49 @@

#include <cutils/qtaguid.h>
#include <cutils/log.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

extern int set_qtaguid(int sockfd, int tag, uid_t uid) {
extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) {
    char lineBuf[128];
    int fd, cnt = 0;
    int fd, cnt = 0, res = 0;
    uint64_t kTag = (uint64_t)tag << 32;
    snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid);

    LOGV("Tagging Socket with command %s\n", lineBuf);
    /* TODO: Enable after the kernel module is fixed.
    LOGI("Tagging socket %d with tag %llx(%d) for uid %d", sockfd, kTag, tag, uid);
    fd = open("/proc/net/xt_qtaguid/ctrl", O_WRONLY);
    if (fd < 0) {
           return -1;
        return -errno;
    }

    cnt = write(fd, lineBuf, strlen(lineBuf));
    if (cnt < 0) {
        res = -errno;
    }

    close(fd);
    */
    return (cnt>0?0:-1);
    return res;
}

extern int qtaguid_untagSocket(int sockfd) {
    char lineBuf[128];
    int fd, cnt = 0, res = 0;
    snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd);

    LOGI("Untagging socket %d", sockfd);
    fd = open("/proc/net/xt_qtaguid/ctrl", O_WRONLY);
    if (fd < 0) {
        return -errno;
    }

    cnt = write(fd, lineBuf, strlen(lineBuf));
    if (cnt < 0) {
        res = -errno;
    }

    close(fd);
    return res;
}