Why does O_CREAT create files only accessible by administrators? - c++

I have the following simple program:
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <string>
using namespace std;
int main() {
string data { "The quick brown fox jumps over the lazy dog" };
int file_descriptor = open("some_file.txt", O_CREAT | O_WRONLY);
write(file_descriptor, data.c_str(), data.size());
cout << file_descriptor << endl;
return 0;
}
which for the most part works fine - the data is outputted into the file. But per http://linux.die.net/man/2/open , the O_CREAT flag should set the file owner to the effective user ID of the process. I'm compiling/running my application from the terminal, but not with any sort of privileges, so why is the created file only visible to administrators?

Random mischance. When you use O_CREAT, open() is a 3-argument function, which takes the file mode as the third argument.
You should be using:
int fd = open("some_file.txt", O_CREATE | O_WRONLY, 0444);
This will create a file with no write permission for anybody (but which your process will be able to write to).
See <sys/stat.h> for more information about the POSIX symbolic constants to use in place of 0444.

Related

System command failing on Linux C++

In my program I am copying an executable file from one location to another, and then execute the copied file. When the copied file is executed I get a "permission denied" error. But if I restart my program then the file gets executed without a problem. Can someone please help me with the problem? The code below is simple, but demonstrates the problem.
void copyFile(string _from, string _to)
{
std::ifstream src(_from.c_str());
std::ofstream dst(_to.c_str());
dst << src.rdbuf();
}
int main()
{
string original("./exe_file");
string dest_file("./exe_dir/exefile");
system("./exe_dir/exefile"); //Fails on first run because exe_dir does not exist.
//mkdir and copy the file.
mkdir("./exe_dir",S_IRWXO | S_IRWXU | S_IRWXG);
copyFile(original, dest_file);
//Open the file and close it again to flush the attribute cache.
int fd = open(dest_file.c_str(),O_RDONLY);
close(fd);
//The line below fails with system error code 2 (Permission denied) on exefile.
return system("./exe_dir/exefile");
{
I used 'chmod 777 exe_file' on the original file before executing the program, and after running this program the destination also has the same access rights. I can execute it manually just fine. And every subsequent run of the program is successful. Why does it fail on the first run?
You should close file you've created.
See cplusplus.com: std::ifstream::close
Coderz, no idea what problems you are experiencing with your IDE but this works fine for me.
#include <iostream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdlib>
using namespace std;
void copyFile(string _from, string _to)
{
std::ifstream src(_from.c_str());
std::ofstream dst(_to.c_str());
dst << src.rdbuf();
}
int main()
{
string original("./exe_file");
string dest_file("./exe_dir/exefile");
system("./exe_dir/exefile");
if (mkdir("./exe_dir", S_IRWXO | S_IRWXU | S_IRWXG))
perror("mkdir");
copyFile(original, dest_file);
if (chmod("./exe_dir/exefile", S_IRWXU | S_IRWXG | S_IRWXO) == -1)
perror("chmod");
return system("./exe_dir/exefile");
}
Note that exe_file is a simple Hello World binary and the results are
sh: 1: ./exe_dir/exefile: not found
Hello World
where the copied file is
-rwxrwxrwx 1 duck duck 18969 May 9 19:51 exefile
within directory
drwxrwxr-x 2 duck duck 4096 May 9 19:51 exe_dir

Redirection in Linux with dup2() and create() inside a loop

I am running the code below and I cannot redirect to a file. The file is made, but nothing is put into it. If I remove the last dup2(saveout,1) statement, I can create and write into the file, but I cannot get back to the terminal, which is important. As soon as I put the dup2(saveout,1) back in my code, the redirection stops working, but I can get back to the terminal. I do not understand why this is happening. I would like to redirect and go back into the terminal.
main.cpp
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string>
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
using namespace std;
void printmessage() {
printf("this is the message\n");
}
int main(int argc, char** argv) {
int saveout;
int fd;
saveout = dup(1);
for (int i = 0; i < 10; i++) {
fd = creat("/home/carl/example.txt",O_CREAT|O_APPEND);
dup2(fd, 1);
close(fd);
printf("Testing the message");
printmessage();
dup2(saveout,1);
close(saveout);
}
return 0;
}
This is a file rights issue, you should read the man pages of the functions you are using.
creat() takes as first argument the filename, and as second the file creation rights, not its opening mode.
The creat() functions is a simple open() call, with some particular flags, so that you'll just have to set up the rights.
if you want to open your file, and create it if he doesn't exists, use
open(filename, O_CREAT | O_RDWR | O_APPEND, 0600) for example, or
creat(filename, 0600),
which is mostly its equivalent, but you wont be able to append text, as "creat() is equivalent to open() with flags equal to O_CREAT | O_WRONLY | O_TRUNC"
The second dup2(saveout,1); will fail because you closed saveout.
printf is buffered by default. (line-by-line for output to a tty, perhaps differently for output to something else). Before both your calls to dup2(..., 1), you should flush with fflush:
fflush(stdout);

reading linux inode bitmap

I'm going to fetch linux inode bitmaps with c++. I've use this code to fetch super block first:
#include <cstdlib>
#include <linux/ext2_fs.h>
#include <linux/fs.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <fcntl.h>
#include <linux/fs.h>
using namespace std;
/*
*
*/
int main() {
int fd;
char boot[1024];
struct ext2_super_block super_block;
fd = open("/dev/sda1", O_RDONLY);
/* Reads the boot section and the superblock */
read(fd, boot, 1024);
read(fd, &super_block, sizeof (struct ext2_super_block));
/* Prints the Magic Number */
printf("%x\n", super_block.s_magic);
close(fd);
return 0;
}
but every time i run it , i get a error :
In file included from main.cpp:2:0:
/usr/include/linux/ext2_fs.h:181:18: error: ‘S_ISDIR’ was not declared in this scope
/usr/include/linux/ext2_fs.h:183:23: error: ‘S_ISREG’ was not declared in this scope
I couldn't find any good example or tutorial for this.is there anybody to help me?
EDIT :
I've include <linux/stat.h> but still get same error.
#grep -rw S_ISREG /usr/src/linux/include
/usr/src/linux/include/linux/fs.h: if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/fs.h.~1~: if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/stat.h:#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
So you should find stat.h in yours kernel source tree and include it.
The Linux source code "stat.h" is not the same file as that comes with the C-library. They just happen to have the same name. You will need to set your include path to find the correct stat.h (you may need BOTH, depending on what you are trying to do).

Create a file as executable program in C in Ubuntu

My program receives an executable binary file through a TCP socket.
I need to save this file in to the harddisk as a executable program. File is successfully received but the problem is the default file attribute is being set to non executable.
How to change the file's attribute as executable in C in Ubuntu?
Thank you,
Regards,
Robo
How about int chmod(const char *path, mode_t mode) and int fchmod(int fd, mode_t mode) ?
apropos chmod
man 2 chmod
The most basic example:
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]){
char * fpath = "/path/to/binary";
int ret=0;
if(ret = chmod(fpath, S_IRUSR|S_IXUSR) < 0){
perror("chmod failed");
exit(1);
}
printf("chmod ok\n");
exit(0);
}
How are you creating and writing the file? If you know it's going to be executable, just make the file with the correct mode in the first place.
int fd = open("path/to/file", O_WRONLY | O_CREAT, 0777);
Unless umask is stripping the executable bits (common values are 0022 and 0002 which leave the executable bits alone), path/to/file will be created initially executable.
You can change the file mode with chmod. Read the man page(man 2 chmod) for detail(roughly the same as shell command chmod).

How to neglect the umask so as to create the file with given permission

I am creating a file using open function and using O_CREAT | O_EXCEL . I have passed the mode as "0666" . But by masking finally the permission allotted to it is -rw-r--r-- and not the
-rw-rw-rw- . Someone told me i can use umask (011) and then reset the original mask again .
But i dont know how to pass this in c++ program. This is the small snippet of What i am doing .
# include <iostream>
# include <stdio.h>
# include <conio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
int main()
{
int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);
getch();
return 0;
}
creates file C:\Users\Ritesh\Music\music.txt with permission -rw-r--r-- . I want it to be -rw-rw-rw-
mode_t old_mask;
old_mask = umask(011);
open( ... );
umask(old_mask);
umask means permissions that you don't want to give to files by default. So if you want to control the permissions completely while creating a file, set umask to 0, which tells the os don't reserve any permissions and let you call the shot. like this:
int main()
{
mode_t oldmask = umask(0);
int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);
close(fd);
umask(oldmask);
getch();
return 0;
}
The only thread-safe way to set file permissions to be what you want is to set them explicitly with chmod() or fchmod() after creating the file (example without error checking):
int fd = open("C:\\Users\\Ritesh\\Music\\music.txt", O_CREAT | O_EXCL, 0666);
fchmod(fd, 0666 );
If you use umask(), you will change the umask value for the entire process. If any other threads are running you risk files getting created with unexpected permissions, which could lead to a security issue or other problems. And any child process created while your changed umask value is in effect will be created with an unexpected umask value.