bonic: libc: cpuacct support for setuid functions

Any of the setuid functions now updates /acct/uid/ with its own tid
before changing users. This is so we can properly account for cpu time
per uid.

Change-Id: I34186cf4d5228cac8439e582a9e26c01ef3011e4
Signed-off-by: Mike Chan <mike@android.com>
This commit is contained in:
Mike Chan 2010-03-02 10:55:58 -08:00
parent bd9a90c443
commit 9f6915631b
21 changed files with 219 additions and 43 deletions

View File

@ -47,7 +47,10 @@ libc_common_src_files := \
unistd/sbrk.c \
unistd/send.c \
unistd/setegid.c \
unistd/setuid.c \
unistd/seteuid.c \
unistd/setreuid.c \
unistd/setresuid.c \
unistd/setpgrp.c \
unistd/sigblock.c \
unistd/siginterrupt.c \
@ -219,6 +222,7 @@ libc_common_src_files := \
bionic/__errno.c \
bionic/__set_errno.c \
bionic/_rand48.c \
bionic/cpuacct.c \
bionic/arc4random.c \
bionic/basename.c \
bionic/basename_r.c \

View File

@ -42,7 +42,7 @@ pid_t __sys_clone:clone (int, void*, int*, void*, int*) 120
int execve (const char*, char* const*, char* const*) 11
int setuid:setuid32 (uid_t) 213
int __setuid:setuid32 (uid_t) 213
uid_t getuid:getuid32 () 199
gid_t getgid:getgid32 () 200
uid_t geteuid:geteuid32 () 201
@ -56,8 +56,8 @@ pid_t getppid() 64
pid_t setsid() 66
int setgid:setgid32(gid_t) 214
int seteuid:seteuid32(uid_t) stub
int setreuid:setreuid32(uid_t, uid_t) 203
int setresuid:setresuid32(uid_t, uid_t, uid_t) 208
int __setreuid:setreuid32(uid_t, uid_t) 203
int __setresuid:setresuid32(uid_t, uid_t, uid_t) 208
int setresgid:setresgid32(gid_t, gid_t, gid_t) 210
void* __brk:brk(void*) 45
# see comments in arch-arm/bionic/kill.S to understand why we don't generate an ARM stub for kill/tkill

View File

@ -6,7 +6,7 @@ syscall_src += arch-arm/syscalls/__fork.S
syscall_src += arch-arm/syscalls/waitid.S
syscall_src += arch-arm/syscalls/__sys_clone.S
syscall_src += arch-arm/syscalls/execve.S
syscall_src += arch-arm/syscalls/setuid.S
syscall_src += arch-arm/syscalls/__setuid.S
syscall_src += arch-arm/syscalls/getuid.S
syscall_src += arch-arm/syscalls/getgid.S
syscall_src += arch-arm/syscalls/geteuid.S
@ -19,8 +19,8 @@ syscall_src += arch-arm/syscalls/getpgid.S
syscall_src += arch-arm/syscalls/getppid.S
syscall_src += arch-arm/syscalls/setsid.S
syscall_src += arch-arm/syscalls/setgid.S
syscall_src += arch-arm/syscalls/setreuid.S
syscall_src += arch-arm/syscalls/setresuid.S
syscall_src += arch-arm/syscalls/__setreuid.S
syscall_src += arch-arm/syscalls/__setresuid.S
syscall_src += arch-arm/syscalls/setresgid.S
syscall_src += arch-arm/syscalls/__brk.S
syscall_src += arch-arm/syscalls/__ptrace.S

View File

@ -2,12 +2,12 @@
#include <sys/linux-syscalls.h>
.text
.type setresuid, #function
.globl setresuid
.type __setresuid, #function
.globl __setresuid
.align 4
.fnstart
setresuid:
__setresuid:
.save {r4, r7}
stmfd sp!, {r4, r7}
ldr r7, =__NR_setresuid32

View File

@ -2,12 +2,12 @@
#include <sys/linux-syscalls.h>
.text
.type setreuid, #function
.globl setreuid
.type __setreuid, #function
.globl __setreuid
.align 4
.fnstart
setreuid:
__setreuid:
.save {r4, r7}
stmfd sp!, {r4, r7}
ldr r7, =__NR_setreuid32

View File

@ -2,12 +2,12 @@
#include <sys/linux-syscalls.h>
.text
.type setuid, #function
.globl setuid
.type __setuid, #function
.globl __setuid
.align 4
.fnstart
setuid:
__setuid:
.save {r4, r7}
stmfd sp!, {r4, r7}
ldr r7, =__NR_setuid32

View File

@ -7,7 +7,7 @@ syscall_src += arch-sh/syscalls/_waitpid.S
syscall_src += arch-sh/syscalls/waitid.S
syscall_src += arch-sh/syscalls/__sys_clone.S
syscall_src += arch-sh/syscalls/execve.S
syscall_src += arch-sh/syscalls/setuid.S
syscall_src += arch-sh/syscalls/__setuid.S
syscall_src += arch-sh/syscalls/getuid.S
syscall_src += arch-sh/syscalls/getgid.S
syscall_src += arch-sh/syscalls/geteuid.S
@ -20,8 +20,8 @@ syscall_src += arch-sh/syscalls/getpgid.S
syscall_src += arch-sh/syscalls/getppid.S
syscall_src += arch-sh/syscalls/setsid.S
syscall_src += arch-sh/syscalls/setgid.S
syscall_src += arch-sh/syscalls/setreuid.S
syscall_src += arch-sh/syscalls/setresuid.S
syscall_src += arch-sh/syscalls/__setreuid.S
syscall_src += arch-sh/syscalls/__setresuid.S
syscall_src += arch-sh/syscalls/setresgid.S
syscall_src += arch-sh/syscalls/__brk.S
syscall_src += arch-sh/syscalls/kill.S

View File

@ -2,11 +2,11 @@
#include <sys/linux-syscalls.h>
.text
.type setresuid, @function
.globl setresuid
.type __setresuid, @function
.globl __setresuid
.align 4
setresuid:
__setresuid:
/* invoke trap */
mov.l 0f, r3 /* trap num */

View File

@ -2,11 +2,11 @@
#include <sys/linux-syscalls.h>
.text
.type setreuid, @function
.globl setreuid
.type __setreuid, @function
.globl __setreuid
.align 4
setreuid:
__setreuid:
/* invoke trap */
mov.l 0f, r3 /* trap num */

View File

@ -2,11 +2,11 @@
#include <sys/linux-syscalls.h>
.text
.type setuid, @function
.globl setuid
.type __setuid, @function
.globl __setuid
.align 4
setuid:
__setuid:
/* invoke trap */
mov.l 0f, r3 /* trap num */

View File

@ -7,7 +7,7 @@ syscall_src += arch-x86/syscalls/_waitpid.S
syscall_src += arch-x86/syscalls/waitid.S
syscall_src += arch-x86/syscalls/__sys_clone.S
syscall_src += arch-x86/syscalls/execve.S
syscall_src += arch-x86/syscalls/setuid.S
syscall_src += arch-x86/syscalls/__setuid.S
syscall_src += arch-x86/syscalls/getuid.S
syscall_src += arch-x86/syscalls/getgid.S
syscall_src += arch-x86/syscalls/geteuid.S
@ -20,8 +20,8 @@ syscall_src += arch-x86/syscalls/getpgid.S
syscall_src += arch-x86/syscalls/getppid.S
syscall_src += arch-x86/syscalls/setsid.S
syscall_src += arch-x86/syscalls/setgid.S
syscall_src += arch-x86/syscalls/setreuid.S
syscall_src += arch-x86/syscalls/setresuid.S
syscall_src += arch-x86/syscalls/__setreuid.S
syscall_src += arch-x86/syscalls/__setresuid.S
syscall_src += arch-x86/syscalls/setresgid.S
syscall_src += arch-x86/syscalls/__brk.S
syscall_src += arch-x86/syscalls/kill.S

View File

@ -2,11 +2,11 @@
#include <sys/linux-syscalls.h>
.text
.type setresuid, @function
.globl setresuid
.type __setresuid, @function
.globl __setresuid
.align 4
setresuid:
__setresuid:
pushl %ebx
pushl %ecx
pushl %edx

View File

@ -2,11 +2,11 @@
#include <sys/linux-syscalls.h>
.text
.type setreuid, @function
.globl setreuid
.type __setreuid, @function
.globl __setreuid
.align 4
setreuid:
__setreuid:
pushl %ebx
pushl %ecx
mov 12(%esp), %ebx

View File

@ -2,11 +2,11 @@
#include <sys/linux-syscalls.h>
.text
.type setuid, @function
.globl setuid
.type __setuid, @function
.globl __setuid
.align 4
setuid:
__setuid:
pushl %ebx
mov 8(%esp), %ebx
movl $__NR_setuid32, %eax

60
libc/bionic/cpuacct.c Normal file
View File

@ -0,0 +1,60 @@
/*
* Copyright (C) 2010 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
//#include <sys/types.h>
int cpuacct_add(uid_t uid)
{
int count;
FILE *fp;
char buf[80];
count = snprintf(buf, sizeof(buf), "/acct/uid/%d/tasks", uid);
fp = fopen(buf, "w+");
if (!fp) {
/* Note: sizeof("tasks") returns 6, which includes the NULL char */
buf[count - sizeof("tasks")] = 0;
if (mkdir(buf, 0775) < 0)
return -errno;
/* Note: sizeof("tasks") returns 6, which includes the NULL char */
buf[count - sizeof("tasks")] = '/';
fp = fopen(buf, "w+");
}
if (!fp)
return -errno;
fprintf(fp, "0");
if (fclose(fp))
return -errno;
return 0;
}

View File

@ -43,6 +43,14 @@ int fork(void)
ret = __fork();
if (ret != 0) { /* not a child process */
__timer_table_start_stop(0);
} else {
/*
* Newly created process must update cpu accounting.
* Call cpuacct_add passing in our uid, which will take
* the current task id and add it to the uid group passed
* as a parameter.
*/
cpuacct_add(getuid());
}
return ret;
}

View File

@ -12,7 +12,7 @@ pid_t _waitpid (pid_t, int*, int, struct rusage*);
int waitid (int, pid_t, struct siginfo_t*, int,void*);
pid_t __sys_clone (int, void*, int*, void*, int*);
int execve (const char*, char* const*, char* const*);
int setuid (uid_t);
int __setuid (uid_t);
uid_t getuid (void);
gid_t getgid (void);
uid_t geteuid (void);
@ -26,8 +26,8 @@ pid_t getppid (void);
pid_t setsid (void);
int setgid (gid_t);
int seteuid (uid_t);
int setreuid (uid_t, uid_t);
int setresuid (uid_t, uid_t, uid_t);
int __setreuid (uid_t, uid_t);
int __setresuid (uid_t, uid_t, uid_t);
int setresgid (gid_t, gid_t, gid_t);
void* __brk (void*);
int kill (pid_t, int);

View File

@ -29,5 +29,6 @@
int seteuid(uid_t euid)
{
return setresuid(-1, euid,-1);
cpuacct_add(euid);
return __setresuid(-1, euid,-1);
}

35
libc/unistd/setresuid.c Normal file
View File

@ -0,0 +1,35 @@
/*
* Copyright (C) 2010 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
int setresuid(uid_t ruid, uid_t euid, uid_t suid)
{
cpuacct_add(euid);
return __setresuid(ruid, euid, suid);
}

34
libc/unistd/setreuid.c Normal file
View File

@ -0,0 +1,34 @@
/*
* Copyright (C) 2010 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
int setreuid(uid_t ruid, uid_t euid)
{
cpuacct_add(euid);
return __setreuid(ruid, euid);
}

34
libc/unistd/setuid.c Normal file
View File

@ -0,0 +1,34 @@
/*
* Copyright (C) 2010 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
int setuid(uid_t uid)
{
cpuacct_add(uid);
return __setuid(uid);
}