LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2023, 06:47 AM   #1
vlrk
Member
 
Registered: Dec 2008
Posts: 51

Rep: Reputation: 1
cap_get_flag usage giving invalid parameter


Hi ,

I am trying out simple example for cap_get_flag.

setting some capability and in next line try to get the same .

some how it is giving a error.

Not able to know, what exactly missing here.

Any pointers would be highly helpfull.

Code:
#include <stdio.h>
#include <sys/capability.h>
#include <sys/types.h>
#include <syslog.h>

int main() {

    cap_t caps;
    cap_value_t cap_list[3];

    cap_list[0] = CAP_SETUID;
    cap_list[1] = CAP_SETGID;
    cap_list[2] = CAP_NET_ADMIN;
    caps = cap_get_proc();

    if(caps != NULL) {
        cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_list, CAP_SET);
        cap_set_flag(caps, CAP_INHERITABLE, 2, cap_list, CAP_SET);
        cap_set_flag(caps, CAP_PERMITTED, 2, cap_list, CAP_SET);
        cap_set_proc(caps);
    } else {
        syslog(LOG_DEBUG, "Cap_get_proc() failed");
    }

    if (caps == NULL) {
        perror("cap_get_proc");
        return 1;
    }

    cap_flag_value_t flag_value;
    if (cap_get_flag(caps, CAP_EFFECTIVE, CAP_NET_ADMIN, &flag_value) == -1) {
        perror("cap_get_flag");
        return 1;
    }

    if (flag_value == CAP_SET) {
        printf("CAP_NET_BIND_SERVICE capability is set.\n");
    } else {
        printf("CAP_NET_BIND_SERVICE capability is not set.\n");
    }

    cap_free(caps);
    return 0;
}
output:
[root@141179 cprog]# ./a.out
cap_get_flag: Invalid argument
 
Old 10-31-2023, 08:21 AM   #2
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,917

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
What a horrid API.

I think you have the CAP_EFFECTIVE, CAP_NET_ADMIN args the wrong way around on the "get" call.
 
1 members found this post helpful.
Old 10-31-2023, 09:25 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Yes, this order seems working:
Code:
    if (cap_get_flag(caps, CAP_NET_ADMIN, CAP_EFFECTIVE, &flag_value) == -1) {
        perror("cap_get_flag");
        return 1;
    }
 
Old 10-31-2023, 11:40 AM   #4
vlrk
Member
 
Registered: Dec 2008
Posts: 51

Original Poster
Rep: Reputation: 1
Thanks it worked.
 
Old 11-01-2023, 01:11 AM   #5
vlrk
Member
 
Registered: Dec 2008
Posts: 51

Original Poster
Rep: Reputation: 1
continuing on the same.

My goal is to set some capabilities to process dynamically and then using setuid and setgid go to non root.
So that some of the capabilities can be still there to the process even after turns in to non root.

But it does not reflect in the cap_get_flag or /proc/<pid>/status in the CapEff.

below is the complete code

Code:
#include <stdio.h>
#include <sys/capability.h>
#include <sys/types.h>
#include <syslog.h>
#include <unistd.h>

int main() {

    cap_t caps,capsg;
    cap_value_t cap_list[3];

    cap_list[0] = CAP_SETUID;
    cap_list[1] = CAP_SETGID;
    cap_list[2] = CAP_NET_ADMIN;
    caps = cap_get_proc();

    if(caps != NULL) {
        cap_set_flag(caps, CAP_EFFECTIVE, 3, cap_list, CAP_SET);
        cap_set_flag(caps, CAP_INHERITABLE, 3, cap_list, CAP_SET);
        cap_set_flag(caps, CAP_PERMITTED, 3, cap_list, CAP_SET);
        cap_set_proc(caps);
    } else {
        syslog(LOG_DEBUG, "Cap_get_proc() failed");
    }

    if (caps == NULL) {
            perror("cap_get_proc");
            return 1;
    }

    if (!setgid(500)) {
            printf("Success in setting Srvr to non root group: euid %d egid %d \n",geteuid(), getegid());
            if (!setuid(2006)) {
                    printf("Success in setting Srvr to non root user :euid %d egid %d \n",geteuid(), getegid());
            } else {
                    printf("Failure in setting Srvr back to root group after setuid failure :euid %d egid %d \n",geteuid(), getegid());
            }
    } else {
            printf("Failure in setting Srvr to non root group,continuing with root user \n");
    }


    capsg = cap_get_proc();

    cap_flag_value_t flag_value;
    if (cap_get_flag(capsg, CAP_NET_ADMIN, CAP_EFFECTIVE, &flag_value) == -1) {
        perror("cap_get_flag");
        return 1;
    }

    if (flag_value == CAP_SET) {
        printf("CAP_NET_BIND_SERVICE capability is set.\n");
    } else {
        printf("CAP_NET_BIND_SERVICE capability is not set.\n");
    }
    sleep(10000);
    cap_free(caps);
    cap_free(capsg);
    return 0;
}
output:
Success in setting Srvr to non root group: euid 0 egid 500
Success in setting Srvr to non root user :euid 2006 egid 500
CAP_NET_BIND_SERVICE capability is not set.


Any pointers / leads will help me here.
 
Old 11-01-2023, 05:15 AM   #6
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,917

Rep: Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035Reputation: 5035
According to capabilities(7), capability sets are reset on a 0 -> non-zero UID transition (amongst other happenings) unless you use prctl() to set certain flags on the process. I'd start by giving that man-page a good read.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell getopts: opt w/ optional parameter is taking next opt as its parameter! GrapefruiTgirl Programming 22 10-27-2010 06:00 AM
Predefined function parameter giving problems. RHLinuxGUY Programming 1 01-31-2007 10:36 PM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
X Error: BadWindow invalid Window parameter 1702fp Slackware 7 10-27-2005 09:51 PM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration