Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. A design problem in my program

A design problem in my program

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 869 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MDCatoM Offline
    MDCatoM Offline
    MDCato
    wrote on last edited by
    #1

    I'm building a GUI program in qt for Linux that runs as a standard user, but occasionally needs to write to a file with root privileges, but as soon as it write the file I need it to drop those privileges. My initial intent was to use QProcess with pkexec to implement the privilege handling, but I don't know how to go from QByteArray of bytes to the actual file in a nice Qt way, I mean I could just write all the bytes to the command line chain, but that just seem really hacky. Anyone know of any examples/have any ideas how to design this?

    Thanks

    jsulmJ 1 Reply Last reply
    0
    • MDCatoM MDCato

      I'm building a GUI program in qt for Linux that runs as a standard user, but occasionally needs to write to a file with root privileges, but as soon as it write the file I need it to drop those privileges. My initial intent was to use QProcess with pkexec to implement the privilege handling, but I don't know how to go from QByteArray of bytes to the actual file in a nice Qt way, I mean I could just write all the bytes to the command line chain, but that just seem really hacky. Anyone know of any examples/have any ideas how to design this?

      Thanks

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @MDCato You could first write to a normal (not privileged) file and use pkexec to copy this file to the privileged location. If you need to append you could also do "cat file1 >> file2" where file2 is the one where you need root.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • MDCatoM Offline
        MDCatoM Offline
        MDCato
        wrote on last edited by MDCato
        #3

        I was considering this as a possibility, however, this would create a race condition, where if someone was clever enough to alter the intermediate file quickly enough before the final write, that would be a vulnerability. I forgot to say that I would like to make this as secure as possible, but Thank you for your help.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Can you explain a bit the context ? That might help spark ideas.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • MDCatoM Offline
            MDCatoM Offline
            MDCato
            wrote on last edited by
            #5

            Okay, I have a main Qt GUI program that is being run as "user1", as it should not need any elevated privileges during most of the programs execution. However, it will need to occasionally write data it generates to a privileged file location, at which point the user will need to approve the temporary privilege elevation (through the system's popup). After the GUI finished the write it will remove the privilege elevation and return to normal execution.

            I figured out what this is, it's Inter-Process-Communcation (IPC), and there's several ways to do this, one way that I'm, leaning towards is QSharedMemory. I believe it allows 2 separate processes to share data between them, and each process can be running with different permissions, I will have to dig deeper into how to use it, but I believe that I can run my main program as user1, and run a second Qt non-GUI program as root, and use QSharedMemory to pass data from the first program to the second program. without changing the execution permissions of the first program and the root program can do the writing to the file.

            JonBJ 1 Reply Last reply
            0
            • MDCatoM MDCato

              Okay, I have a main Qt GUI program that is being run as "user1", as it should not need any elevated privileges during most of the programs execution. However, it will need to occasionally write data it generates to a privileged file location, at which point the user will need to approve the temporary privilege elevation (through the system's popup). After the GUI finished the write it will remove the privilege elevation and return to normal execution.

              I figured out what this is, it's Inter-Process-Communcation (IPC), and there's several ways to do this, one way that I'm, leaning towards is QSharedMemory. I believe it allows 2 separate processes to share data between them, and each process can be running with different permissions, I will have to dig deeper into how to use it, but I believe that I can run my main program as user1, and run a second Qt non-GUI program as root, and use QSharedMemory to pass data from the first program to the second program. without changing the execution permissions of the first program and the root program can do the writing to the file.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @MDCato
              I will just throw this in for your consideration.

              You can use pkexec and/or separate privileged process with some IPC if that is your preference. But since you are Linux you can also use the standard, original way of doing this by installing your executable setuid. You then swap to realuid upon start-up, and only go setuid (effective used id) while you need to write the permission-restricted file, switching immediately back to realuid upon completion.

              Qt itself does not provide an interface to Linux setuid calls, but nothing stopping you calling the functions yourself, there are times when you do platform-specific things in your Qt application.

              I'm not going to get into the pro & cons of setuid vs pkexec vs IPC to a demon process. You can read up on that yourself from the web, which you should do. I'll just say that with setuid you would do everything from your Qt app , so there would be no issues about passing the desired file content to another process.

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Since it's in a privileged location, do you have something else accessing that file ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved