I am trying to test the "capset" call in ubuntu system. This is the codes:
int cap_mask
cap_header.pid = getpid() ;
cap_header.version = _LINUX_CAPABILITY_VERSION_3;
if( capget(&cap_header, &cap_data) < 0)
{
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("capheader: %x %d\n", cap_header.version, cap_header.pid);
printf("capdata: %x %x %x\n", cap_data.effective, cap_data.permitted, cap_data.inheritable);
cap_mask |= (1 << CAP_NET_BIND_SERVICE);
cap_data.effective = cap_mask;
cap_data.permitted = cap_mask;
cap_data.inheritable = 0;
if( capset(&cap_header, &cap_data) < 0)
{
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
printf("%d\n", capget(&cap_header, &cap_data));
printf("capheader: %x %d\n", cap_header.version, cap_header.pid);
printf("capdata: %x %x %x\n", cap_data.effective, cap_data.permitted, cap_data.inheritable);
return 0;
After running the binary, the outputs are:
capheader: 20080522 24315
capdata: 0 0 0
Operation not permitted
It seems that the capset fail with the error "operation not permitted" While if I comment this line
/*cap_mask | = (1<< CAP_NET_BIND_SERVICE)*/
The call capset will succeed with the outputs:
capheader: 20080522 24464
capdata: 0 0 0
0
capheader: 20080522 24464
capdata: 0 0 0
Do you know why the capset fail at the 1st run?