1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/*
Copyright (C) (2004 - 2006) (Venkata Ramana Enaganti) <ramana@intraperson.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
For dropping root capabilities and to
keep only those that are required
all forked procs -- all backup procs will have only CAP_DAC_READ_SEARCH.
*/
#include <sys/capability.h>
#include "msg.h"
#include "dropcap.h"
#ifdef DISPLAY_CAP
static void dropcap_discap( void )
{
char *txt;
cap_t ct;
if( ! ( ct = cap_get_proc() ) )
{
msglog( MSG_ERR|LOG_ERRNO, "dropcap_discap: cap_get_proc" );
return;
}
if( ! ( txt = cap_to_text( ct, NULL ) ) )
{
msglog( MSG_ERR, "dropcap_discap: cap_to_text" );
cap_free( ct );
return;
}
msglog( MSG_INFO, "remaining capabilities: %s", txt );
cap_free( txt );
cap_free( ct );
}
#endif
void dropcap_drop( void )
{
cap_t ct;
const char *caps =
/* CAP_CHOWN
This overrides the restriction of changing file
ownership and group ownership.
*/
"cap_chown," \
/* CAP_DAC_OVERRIDE
Override all DAC access.
*/
"cap_dac_override," \
/* CAP_FOWNER
Overrides all restrictions about allowed operations
on files, where file owner ID must be equal to the user ID,
except where CAP_FSETID is applicable.
It doesn't override MAC and DAC restrictions.
*/
"cap_fowner," \
/* CAP_CHOWN
Overrides the following restrictions that the
effective user ID shall match the file owner ID
when setting the S_ISUID and S_ISGID bits on that
file; that the effective group ID (or one of
the supplementary group IDs) shall match the file
owner ID when setting the S_ISGID bit on that file
*/
"cap_fsetid," \
/* CAP_KILL
Overrides the restriction that the real or effective
user ID of a process sending a signal must match
the real or effective user ID of the process receiving the signal.
"cap_kill," \
*/
/* CAP_SYS_ADMIN -- too much power but
we are interested only in the following,
Allow mount() and umount()
Allow some autofs root ioctls
*/
"cap_sys_admin+ep" \
" " \
/* CAP_DAC_READ_SEARCH -- for backup;
Overrides all DAC restrictions regarding read and
search on files and directories.
*/
"cap_dac_read_search+epi";
if( ! ( ct = cap_from_text( caps ) ) )
msglog( MSG_FATAL|LOG_ERRNO, "dropcap_drop: cap_from_text" );
msglog( MSG_INFO, "giving up unnecessary root privileges" );
if( cap_set_proc( ct ) )
{
msglog( MSG_ERR|LOG_ERRNO, "dropcap_drop: cap_set_proc" );
msglog( MSG_WARNING, "could not drop root privileges" );
}
cap_free( ct );
#ifdef DISPLAY_CAP
/*enable this if you are too paranoid*/
dropcap_discap();
#endif
}
|