Sertac TULLUK
Sertac TULLUK

Reputation: 89

What capabilities required for ioctl() on emmc on systemd?

I want to run my program with systemd with a regular user ( non-root). This program uses ioctl() syscall to access emmc registers.I want to learn which capabilities required to be added to my systemd unit file.

I tried with below unit file:

[Unit]
Description=EMMC-LIFETIME UTILITY

[Service]
User=tron
Group=disk
ExecStart=/HARICI/emmc-lifetime /dev/mmcblk0 -v
CapabilityBoundingSet=CAP_SYS_ADMIN
DeviceAllow=/dev/mmcblk0 rw

[Install]
WantedBy=multi-user.target

Here is the code of emmc-lifetime:

int main(int argc, char **argv)
{

    if(argc < 2){
        printf("Usage: %s <mmcfilename> (-v)\n", argv[0]);
        printf("Example: %s /dev/mmcblk1 -v\n", argv[0]);
        return 1;
    }

    char ext_csd[512], ext_csd_rev;
    int fd, ret;

    fd = open(argv[1], O_RDWR);
    if (fd < 0) {
        printf("Failed to open eMMC device, please check which path you have passed\n");
        return 1;
    }

    struct mmc_ioc_cmd idata;
    memset(&idata, 0, sizeof(idata));
    memset(ext_csd, 0, sizeof(char) * 512);
    idata.write_flag = 0;
    idata.opcode = MMC_SEND_EXT_CSD;
    idata.arg = 0;
    idata.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
    idata.blksz = 512;
    idata.blocks = 1;
    mmc_ioc_cmd_set_data(idata, ext_csd);

    ret = ioctl(fd, MMC_IOC_CMD, &idata);
    if (ret){
        printf("ioctl failed, are you sure it is an MMC device???\n");
        return ret;
    }


    ext_csd_rev = ext_csd[EXT_CSD_REV];

    if (ext_csd_rev >= 7) {

        if(argc==3 && !strcmp(argv[2],"-v")){
        printf("EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A: 0x%02x\n",
            ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A]);
        printf("EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B: 0x%02x\n",
            ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]);
        printf("EXT_CSD_PRE_EOL_INFO: 0x%02x\n",
            ext_csd[EXT_CSD_PRE_EOL_INFO]);
        }else{
            printf("%d\n",ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B]*10);
        }
    }

    if(fd)
        close(fd);

    return ret;

If I comment out "User=tron" in my unit file, everything works expectedly:

Nov 03 01:17:03 tron systemd[1]: Started EMMC-LIFETIME UTILITY.
Nov 03 01:17:03 tron emmc-lifetime[28294]: EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A: 0x01 
Nov 03 01:17:03 tron emmc-lifetime[28294]: EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B: 0x01 
Nov 03 01:17:03 tron emmc-lifetime[28294]: EXT_CSD_PRE_EOL_INFO: 0x01 

But if I uncomment "User=tron", here is the result:

Nov 03 00:57:17 tron systemd[1]: Started EMMC-LIFETIME UTILITY. 
Nov 03 00:57:17 tron emmc-lifetime[27706]: ioctl failed, are you sure it is an MMC device??? 
Nov 03 00:57:17 tron systemd[1]: emmc-info.service: Main process exited, code=exited, status=255/n/a 
Nov 03 00:57:18 tron systemd[1]: emmc-info.service: Unit entered failed state. 
Nov 03 00:57:18 tron systemd[1]: emmc-info.service: Failed with result 'exit-code'.

What capabilities are required in my unit file to run my executable as "tron" user ?

Upvotes: 0

Views: 1176

Answers (1)

Sertac TULLUK
Sertac TULLUK

Reputation: 89

Solved. For the capabilities side, it must have CAP_SYS_RAWIO.

We can use :

setcap cap_sys_rawio=+eip /HARICI/emmc-lifetime

And by this way, we can run "emmc-lifetime" binary with non-root user. Please note that, this user must be in "disk" group to be able to open "/dev/mmcblkX"

But unfortunately, this doesn't work in systemd unit files.

In systemd unit files, if you write:

User=some-non-root-user
CapabilityBoundingSet=SOME_CAPABILITY

Does not work. Thats why my above code allways fails.

I must find another way to run my binary with non-root privileges.

Upvotes: 0

Related Questions