Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 682203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:34:10+00:00 2026-05-14T01:34:10+00:00

Changing a Linux C++ program which gives the user limited file access. Thus the

  • 0

Changing a Linux C++ program which gives the user limited file access. Thus the program chroots itself to a sandbox with the files the user can get at. All worked well.

Now, however, the program needs to access some files for its own needs (not the user’s) but they are outside the sandbox. I know chroot allows access to files opened before the chroot but in this case the needed files could a few among many hundreds so it is obviously impractical to open them all just for the couple that might be required.

Is there any way to get at the files?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:34:10+00:00Added an answer on May 14, 2026 at 1:34 am

    If the files you need to access are within a few directories you could open those directories before you chroot and save the file descriptors. You can then use the so-called *at functions (e.g. openat(), renameat(), etc.) to get at the individual files. Basically you are opening the files relative to the already open directory file descriptors rather than the chrooted directory.

    Whether this is a safe thing to do is open to question but it should work in Linux.

    EDIT: This is on the ugly side but it seems to work. You should poke around a lot more for vulnerabilities than I have. I haven’t tested how dropping privileges and so forth will effect things.

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #include <cstdio>
    #include <cstdlib>
    #include <cerrno>
    #include <cstring>
    
    #include <unistd.h>
    #include <fcntl.h>
    #include <dirent.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    
    int main(int argc, char *argv[])
    {
        if (argc < 4)
        {
            cerr << "USAGE: " << argv[0] << " <jail directory> <freeworld directory> <filename>\n";
            exit(EXIT_FAILURE);
        }
    
        const string JAILDIR(argv[1]);
        const string FREEDIR(argv[2]);
        string freefilename(argv[3]);
    
        while (freefilename[0] == '/')
            freefilename.erase(0, 1);
    
        DIR *pDir;
    
        if ((pDir = opendir(FREEDIR.c_str())) == NULL)
        {
            perror("Could not open outside dir");
            exit(EXIT_FAILURE);
        } 
    
        int freeFD = dirfd(pDir);
    
        //cd to jail dir
        if (chdir(JAILDIR.c_str()) == -1)
        {
            perror("cd before chroot");
            exit(EXIT_FAILURE);
        }
    
        //lock in jail
        if (chroot(JAILDIR.c_str()) < 0)
        {
            cerr << "Failed to chroot to " << JAILDIR << " - " << strerror(errno) << endl;
            exit(EXIT_FAILURE);
        }
    
        //
        //in jail, won't work
        //
    
        string JailFile(FREEDIR);
        JailFile += "/";
        JailFile += freefilename;
    
        int jailFD;
    
        if ((jailFD = open(JailFile.c_str(), O_RDONLY)) == -1)
        {
            cout << "as expected, could not open " << JailFile << endl;
            perror("exected open fail");
        }
        else
        {
            cout << "defying all logic, opened " << JailFile << endl;
            exit(EXIT_FAILURE);
        }
    
        //
        //using this works
        //
    
        if ((jailFD = openat(freeFD, freefilename.c_str(), O_RDONLY)) == -1)
        {
            cout << "example did not work. Could not open " << freefilename << " Sorry!" << endl;
            exit(EXIT_FAILURE);
        }
        else
            cout << "opened " << freefilename << " from inside jail" << endl;
    
        char     buff[255];
        ssize_t  numread;
    
        while (1)
        {
            if ((numread = read(jailFD, buff, sizeof(buff) - 1)) == -1)
            {
                perror("read");
                exit(EXIT_FAILURE);
            }
    
            if (numread == 0)
                break;
    
            buff[numread] = '\0';
            cout << buff << endl;
        }
    
        return 0;
    }
    

    To test:

    echo “Hello World” >/tmp/mystuff.dat

    mkdir /tmp/jail

    sudo ./myprog /tmp/jail /tmp mystuff.dat

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 366k
  • Answers 366k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hmmm... Good question. It seems they can't even have an… May 14, 2026 at 4:35 pm
  • Editorial Team
    Editorial Team added an answer The new object, when inserted into the map by []… May 14, 2026 at 4:35 pm
  • Editorial Team
    Editorial Team added an answer I'm concerned this will hit the same problem and I'm… May 14, 2026 at 4:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.