I am trying to run a script from my c++ code owned by root.
But it throws me an error permission denied to run the script.
Permissions on files are as follows:
-rwx------ 1 root mygame 39 Dec 24 19:11 script.sh
-rwsr-xr-x 1 gag5kor mygame 7999 Dec 26 12:23 a.out
C++ code:
int err = system("./script.sh");
cout << "Before err: " << err << endl;
cout << "setuid: " << seteuid(0) << endl;
err = system("./script.sh");
cout << "After err: " << err << endl;
getuid() and geteuid() function returns me same value (say 1234) even after I call setuid(0) to get the root permissions.
What I am doing wrong here?
I read the other answers on stackoverflow but not able to understand properly.
chmod 0555 script.sh should fix this
EDIT
chown root script.sh
chmod 0500 script.sh
Should fulfill your requirements
Related
Below is my test.cpp file.
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
int main() {
cout << "Content-type:text/html" << endl << endl;
cout << "<html>" << endl;
cout << "<head>" << endl;
cout << "<title>Hello World - First CGI Program</title>" << endl;
cout << "</head>" << endl;
cout << "<body>" << endl;
cout << "<h2>Hello World! This is my first CGI program</h2>" << endl;
cout << "</body>" << endl;
cout << "</html>" << endl;
return 0;
}
Below is how I compiled the test.cpp file to test.cgi file.
g++ -g test.cpp -o test.cgi
Below is how I change mode of the test.cgi file.
chmod 755 test.cgi
There is no error at all.
But when I visit the page in the browser "localhost:8080/test.cgi", I get the following error:
C:/xampp/cgi-bin/test.cgi is not executable; ensure interpreted scripts have "#!" or "'!" first line
[cgi:error] [pid 22568:tid 1864] (9)Bad file descriptor: [client ::1:60380] AH01222: don't know how to spawn child process: C:/xampp/cgi-bin/test.cgi
The default cgi.cgi of Apache server works as well. It seems like the test.cgi file is invalid. Because I cannot even view the content of the file in Visual Studio Code, while I can view the content of the cgi.cgi file as well, the default file of Apache server.
When I run the following command line in the terminal, the content of the test.cgi file is printed successfully.
./test.cgi
How to create a valid cgi file from a cpp file?
i am trying to write a linux program that uses the c++ mount function (code below),
however, the mount operation requires permmissions, and running the program throws the errno 'Operation not permitted' (printed using perror)
tried some SO solutions but non was helpful, the alternative is to use the system("sudo mount..") but i prefer the c++ function.
is ther a way to use this function with permmissions?
IDE: Clion 2020.2.4
relevant code below
int returnValue = mount(sourcePath,targetPath,"", MS_SHARED, ""); //mounting the device
if (returnValue==0){
//mount completed
//somecode
}else{
//mount failed
std::cout<<"mount failed\n";
perror("");
}
output
mount failed
Operation not permitted
After you compile the code, change the ownership of the file to the superuser with chown root filename and add "set user or group ID on execution" to the mode of the executable file with chmod u+s filename.
Some options I see:
Just run the binary as root or under sudo;
Use setcap cap_sys_admin+ep on your binary to grant it the CAP_SYS_ADMIN capability;
If the set of possible targetPaths is fixed, edit /etc/fstab to give these paths the userflag.
#include <fstream>
#include <iostream>
#include <string>
int main(int argc, char *argv[]){
std::ifstream tmpfile;
std::string tmpfile_name = ".mytempfile.tmp";
std::string command = "groups>";
std::string searchv[] = {"disk", "sudo", "root"};
int searchc = sizeof(searchv)/sizeof(searchv[0]);
int search_matches = 0;
char data_buffer[128];
if(!system(NULL)) goto ERROR;
command += tmpfile_name;
if(system(command.c_str()) != 0) goto ERROR;
std::cout << "executed external command: \"" << command << "\"" << std::endl;
tmpfile.open(tmpfile_name, std::ios::in);
if(!tmpfile.is_open()) goto ERROR;
std::cout << tmpfile_name << " opened" << std::endl;
do{
tmpfile >> data_buffer;
if(tmpfile.eof()) break;
if(tmpfile.fail()) goto ERROR;
for(int i = 0; i < searchc; i++){
if(!searchv[i].compare(data_buffer)){
search_matches++;
std::cout << "found group " << searchv[i] << std::endl;
}
}
}
while(tmpfile.good());
tmpfile.close();
std::cout << "found " << search_matches << " groups" << std::endl;
return EXIT_SUCCESS;
ERROR:
std::cerr << "something bad happened" << std::endl;
return EXIT_FAILURE;
}
This answer may be off-topic, sorry for that.
This program calls the external Linux program "groups" and searches for keywords "disk", "sudo", "root", which indicating user access rights for mounting a disk.
accessing an os function implies complying with that os's security model.
so short answer, no. you can't override security models in your user-run code
I am trying to run a c++ program with a txt file as a command line arguement. I used chmod u+rwx filename to change the access and g++ to compile and ./ to run. I am keep getting an error message saying "zsh: permission denied: ./
". I can make unix executable file using g++, bu when I run the progrma with ./ and txt file ans an arguement, it returns an error message above.
what my terminal looks like
-Air ~ % cd /Users/klee/Desktop/pa3+ect/cs1/page-link
-Air page-link % g++ page.cpp page_rank.cpp web.cpp -o rank1
-Air page-link % ./ rank1
zsh: permission denied: ./
-Air page-link %
U can use the standard argument to get the file path and then parse the file text into a variable.
int main(int argc, char** argv)
{
cout << "You have entered " << argc
<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}
The aim of this c++ program is about the understanding the cocurrent process mechanism in operating system. And the following code is for the child functions of one process. And the child process have theirs numbers, NO.5 and NO.6.
I'm trying to exectute an a.out file in the NO.6 process. I'm trying to do it this way.
void ChildFunction_For_ProcessNO.4(int i){
switch(i){
case(5):
cout << "This is process five, and the ID for this process is " << getpid() << '\n'
<< "and the ID for the parent process is " << getppid() << '\n';
CreateThreads_Five();
cout << "Process five has ended.\n" << '\n';
break;
case(6):
cout << "This is process six, and the ID for this process is " << getpid() << '\n'
<< "and the ID for the parent process is " << getppid() << '\n';
execl("./a.out", "a.out", NULL);
//and I also tried this way
execl("Home/CLionProjects/Project_1/a.out", "a.out", NULL);
char buf[100];
cout << "getcwd: " << getcwd(buf, sizeof(buf))) << endl;
cout << "Process six has ended.\n";
break;
}
and the getcwd's output goes like this
getcwd: /home/chengxuyuan/CLionProjects/Project_1/cmake-build-debug
The a.out file has already been put in the folder together with the c++ program.
the screenshot of the working directory
and the compile went well, but there is just no output which ought to be Hello world from the a.out file. How can I solve this problem. Thanks a lot!
The getcwd output shows that you have to put your file a.out into
/home/chengxuyuan/CLionProjects/Project_1/cmake-build-debug
Your attempt
execl("Home/CLionProjects/Project_1/a.out", "a.out", NULL);
is wrong this is not the full path. You would have to use
execl("/home/chengxuyuan/CLionProjects/Project_1/a.out", "a.out", NULL);
BTW: You should specify the same value as argument 0 as you use for the program to be run, i.e.
execl("/home/chengxuyuan/CLionProjects/Project_1/a.out", "/home/chengxuyuan/CLionProjects/Project_1/a.out", NULL);
or
execl("./a.out", "./a.out", NULL);
This is what the shell would do and what most programs expect.
I am using system() to run some Unix commands from my application with code like the following:
std::stringstream command;
command << "rm -rf /some/directory";
int rmResult = system(command.str().c_str());
if (rmResult != 0) {
clog << "Error: Failed to remove old output directory '" << command.str()
<< "' (" << errno << ") " << strerror(errno) << ".\n";
throw;
}
However, while rmResult is zero and the rm works, I get this error in the console:
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
What am I doing wrong, and how can I get this message to go away?
Apparently, this was due to having a directory that is now gone on my pushd stack, even though it was not the current working directory. Cleaning out my stack of the now gone directory, caused the messages to go away.