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. Question about QProcess and deleting a file
Qt 6.11 is out! See what's new in the release blog

Question about QProcess and deleting a file

Scheduled Pinned Locked Moved Solved General and Desktop
28 Posts 5 Posters 16.6k Views 2 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.
  • VRoninV VRonin

    QTemporaryFile (if not leaked) guarantees the file is removed when the program closes (unless you explicitly tell it not to delete it). The problem with it is that if the program crashes or gets killed that file will still live. That's why I suggest managing the life of the file in a separate, very simple program that has very limited chance to crash.

    When I use QTemporaryFile I also always set up a function like this that removes any leftover temporary file from a previous crash and call it at startup

    Edit:

    I really don't want to leave an external file with a password in it lying around in the file system potentially....

    I don't think you can ever guarantee this.

    Edit2:
    If security is really a concern then you'd need to even prevent recovery of the file and this is basically impossible on SSD.
    I think you are concerned about security while a fatal security flaw was introduced by something you have no control over. If you have to live with the vulnerability, roll with it

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

    @VRonin
    Yeah OK.

    I've started to look up what i had in mind. Here's Linux man open(2):

       O_TMPFILE (since Linux 3.11)
             Create  an unnamed temporary file.  The pathname argument specifies a directory; an unnamed  inode  will  be  created  in  that
             directory's  filesystem.  Anything written to the resulting file
             will be lost when the last file descriptor is closed, unless the
             file is given a name.
    

    That's what I had in mind, because you'll see it doesn't matter if the creating process dies. But I just realised this gives the file no name, so it won't be so useful for passing to another program!!

    OK, so man 3 tmpfile:

       The  tmpfile()  function  opens  a  unique  temporary  file  in  binary
      read/write (w+b) mode.  The file will be automatically deleted when  it
      is closed or the program terminates.
    

    This does create a named file, and returns an open handle. Again note the "deleted when program terminates" (assuming it's an OS facility, if they only mean C runtime clean-up it's no good... I need to check, sigh.). I think this is the closest to what I recall...

    As I said, I'm not fanatical over security, I know it will vulnerable, just looking for a reasonable deal.

    1 Reply Last reply
    0
    • JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #14

      OK, I guess I'll have to reveal what this subprocess is!

      mysqldump, to do database backups. It needs username + password to connect to MySQL server as.

      If you look at how MySQL Workbench invokes it, it uses mysqldump's acceptance of command-line:

      mysqldump --defaults-file="/tmp/tmpsYWm9r/extraparams.cnf" ...

      to pass (just) the password in that file. I do not know what flags it might use to open that file.

      Now, at present I am just using command-line:

      mysqldump --user=... --password=... ...

      The output --- which my user sees --- starts with:

      mysqldump: [Warning] Using a password on the command line interface can be insecure.

      I am aware of why that is a risk. It would be dishonest of me to censor that. Although I'm beginning to think that external file is a hassle and has holes, so should I bother to change code to it? But if Workbench chooses to do it this way shouldn't I make the same effort? And if my kind of users see that warning they'll have kittens, so I do think I need to do the external file after all as best I can...

      VRoninV M 2 Replies Last reply
      0
      • JonBJ JonB

        OK, I guess I'll have to reveal what this subprocess is!

        mysqldump, to do database backups. It needs username + password to connect to MySQL server as.

        If you look at how MySQL Workbench invokes it, it uses mysqldump's acceptance of command-line:

        mysqldump --defaults-file="/tmp/tmpsYWm9r/extraparams.cnf" ...

        to pass (just) the password in that file. I do not know what flags it might use to open that file.

        Now, at present I am just using command-line:

        mysqldump --user=... --password=... ...

        The output --- which my user sees --- starts with:

        mysqldump: [Warning] Using a password on the command line interface can be insecure.

        I am aware of why that is a risk. It would be dishonest of me to censor that. Although I'm beginning to think that external file is a hassle and has holes, so should I bother to change code to it? But if Workbench chooses to do it this way shouldn't I make the same effort? And if my kind of users see that warning they'll have kittens, so I do think I need to do the external file after all as best I can...

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #15

        @JNBarchan said in Question about QProcess and deleting a file:

        OK, I guess I'll have to reveal what this subprocess is!
        mysqldump

        That makes everything much more simple. The subprocess has no vulnerability, you can store your credential in encrypted format so, even if the file remains around, it will be just gibberish to any attacher.

        https://dev.mysql.com/doc/refman/5.7/en/mysql-config-editor.html

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        JonBJ 1 Reply Last reply
        2
        • VRoninV VRonin

          @JNBarchan said in Question about QProcess and deleting a file:

          OK, I guess I'll have to reveal what this subprocess is!
          mysqldump

          That makes everything much more simple. The subprocess has no vulnerability, you can store your credential in encrypted format so, even if the file remains around, it will be just gibberish to any attacher.

          https://dev.mysql.com/doc/refman/5.7/en/mysql-config-editor.html

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

          @VRonin
          Unfortunately not :( While I might be prepared to use this approach, my end users are not. They won't have/create the file, they won't be prepared to run the configurer, and they generally will not accept or cooperate.

          I knew about this avenue, and I do respect your suggestion, but the purpose of this question is to emulate just what MySQL Workbench does (as I've shown above) in precisely the same circumstances, i.e. no .mylogin.cnf file at all, let alone encrypted.

          1 Reply Last reply
          0
          • JonBJ JonB

            OK, I guess I'll have to reveal what this subprocess is!

            mysqldump, to do database backups. It needs username + password to connect to MySQL server as.

            If you look at how MySQL Workbench invokes it, it uses mysqldump's acceptance of command-line:

            mysqldump --defaults-file="/tmp/tmpsYWm9r/extraparams.cnf" ...

            to pass (just) the password in that file. I do not know what flags it might use to open that file.

            Now, at present I am just using command-line:

            mysqldump --user=... --password=... ...

            The output --- which my user sees --- starts with:

            mysqldump: [Warning] Using a password on the command line interface can be insecure.

            I am aware of why that is a risk. It would be dishonest of me to censor that. Although I'm beginning to think that external file is a hassle and has holes, so should I bother to change code to it? But if Workbench chooses to do it this way shouldn't I make the same effort? And if my kind of users see that warning they'll have kittens, so I do think I need to do the external file after all as best I can...

            M Offline
            M Offline
            mchinand
            wrote on last edited by
            #17

            I think this might be more secure but I'm not 100% sure. You could use QProcessEnvironment to assign your password to the MYSQL_PWD environment variable.

            JonBJ 1 Reply Last reply
            2
            • M mchinand

              I think this might be more secure but I'm not 100% sure. You could use QProcessEnvironment to assign your password to the MYSQL_PWD environment variable.

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

              @mchinand
              Ooohhh, that's interesting. Where do you get the MYSQL_PWD environment variable documentation from, please?

              M 1 Reply Last reply
              0
              • JonBJ JonB

                @mchinand
                Ooohhh, that's interesting. Where do you get the MYSQL_PWD environment variable documentation from, please?

                M Offline
                M Offline
                mchinand
                wrote on last edited by mchinand
                #19

                @JNBarchan said in Question about QProcess and deleting a file:

                @mchinand
                Ooohhh, that's interesting. Where do you get the MYSQL_PWD environment variable documentation from, please?

                See this part of the QProcess help. Your mysqldump process will use the value of MYSQL_PWD for the password if you don't specify it as a command-line argument. After further searching, it's probably not any more secure since there are ways to get a process' environment according to the MySQL manual (bottom of that page)

                JonBJ 1 Reply Last reply
                1
                • M mchinand

                  @JNBarchan said in Question about QProcess and deleting a file:

                  @mchinand
                  Ooohhh, that's interesting. Where do you get the MYSQL_PWD environment variable documentation from, please?

                  See this part of the QProcess help. Your mysqldump process will use the value of MYSQL_PWD for the password if you don't specify it as a command-line argument. After further searching, it's probably not any more secure since there are ways to get a process' environment according to the MySQL manual (bottom of that page)

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

                  @mchinand
                  I looked at the MySQL manual page, thanks. It was a good idea I didn't know about, and is useful information. Unfortunately, though, it actually describes environment variable as "extremely insecure", one rank down from passing on OS command-line, so probably not. But a good suggestion!

                  mrjjM 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @mchinand
                    I looked at the MySQL manual page, thanks. It was a good idea I didn't know about, and is useful information. Unfortunately, though, it actually describes environment variable as "extremely insecure", one rank down from passing on OS command-line, so probably not. But a good suggestion!

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #21

                    @JNBarchan
                    But on the practical side, using MYSQL_PWD remove the race condition on when to remove the file. ?

                    Also, if you had a small launcher app, that you run from the master
                    GUI, that fires up and starts mysqldump and then terminates, would that not remove the environment and MYSQL_PWD in a very short time frame, making the user ability to list the password using ps way slimmer?

                    Mind you im comparing this with a text file with a plain password that will exit for a second or longer. ( to be on safe side)

                    I mean, a file not hidden from lsof either and if the user have root access, both approaches are equally easy to hax.

                    I assume you will be using a read only user account for backup.

                    JonBJ 1 Reply Last reply
                    0
                    • mrjjM mrjj

                      @JNBarchan
                      But on the practical side, using MYSQL_PWD remove the race condition on when to remove the file. ?

                      Also, if you had a small launcher app, that you run from the master
                      GUI, that fires up and starts mysqldump and then terminates, would that not remove the environment and MYSQL_PWD in a very short time frame, making the user ability to list the password using ps way slimmer?

                      Mind you im comparing this with a text file with a plain password that will exit for a second or longer. ( to be on safe side)

                      I mean, a file not hidden from lsof either and if the user have root access, both approaches are equally easy to hax.

                      I assume you will be using a read only user account for backup.

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

                      @mrjj

                      But on the practical side, using MYSQL_PWD remove the race condition on when to remove the file. ?

                      Yes, but then so would passing --password=... on the command-line.

                      Also, if you had a small launcher app, that you run from the master
                      GUI, that fires up and starts mysqldump and then terminates, would that not remove the environment and MYSQL_PWD in a very short time frame, making the user ability to list the password using ps way slimmer?

                      For the record, no. Because it's using an environment variable passed to the mysqldump executable, it persists in that process's space for the duration, and can be found via ps. In that sense, the command-line argument is actually safer, because mysqldump obscures this immediately after start up.

                      I assume you will be using a read only user account for backup.

                      Umm, no actually. Credentials for MySQL are configured into my app from someone at user's site. Chances are, they only even have one account, and that account may well be the root one, wouldn't have another one for writing to the database let alone reading from it.... Don't shoot me, I'm only the messenger.

                      mrjjM 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @mrjj

                        But on the practical side, using MYSQL_PWD remove the race condition on when to remove the file. ?

                        Yes, but then so would passing --password=... on the command-line.

                        Also, if you had a small launcher app, that you run from the master
                        GUI, that fires up and starts mysqldump and then terminates, would that not remove the environment and MYSQL_PWD in a very short time frame, making the user ability to list the password using ps way slimmer?

                        For the record, no. Because it's using an environment variable passed to the mysqldump executable, it persists in that process's space for the duration, and can be found via ps. In that sense, the command-line argument is actually safer, because mysqldump obscures this immediately after start up.

                        I assume you will be using a read only user account for backup.

                        Umm, no actually. Credentials for MySQL are configured into my app from someone at user's site. Chances are, they only even have one account, and that account may well be the root one, wouldn't have another one for writing to the database let alone reading from it.... Don't shoot me, I'm only the messenger.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #23

                        @JNBarchan
                        Oh, i though the environment would die with the calling process.
                        Yep then clearly it's worse. ( as backup takes comparatively long time)

                        Are you in control of the mysqldump ?
                        I wondering if a user could simply replace the mysqldump executable with own program and be served the password.

                        JonBJ 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @JNBarchan
                          Oh, i though the environment would die with the calling process.
                          Yep then clearly it's worse. ( as backup takes comparatively long time)

                          Are you in control of the mysqldump ?
                          I wondering if a user could simply replace the mysqldump executable with own program and be served the password.

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

                          @mrjj
                          Child processes inherit their own copy of parent/caller's environment, else whenever parent exited without waiting for child to complete child would suddenly lose its environment variables!

                          Theoretically at least, end user cannot use his own mysqldump program because full path can be configured into my app by their administrator.

                          These choices have already been made by MySQL Workbench when it invokes mysqldump. And that has decided to pass password via temporary file. I'm just trying to emulate similar behaviour from Qt, in safest fashion I can think of.

                          mrjjM 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @mrjj
                            Child processes inherit their own copy of parent/caller's environment, else whenever parent exited without waiting for child to complete child would suddenly lose its environment variables!

                            Theoretically at least, end user cannot use his own mysqldump program because full path can be configured into my app by their administrator.

                            These choices have already been made by MySQL Workbench when it invokes mysqldump. And that has decided to pass password via temporary file. I'm just trying to emulate similar behaviour from Qt, in safest fashion I can think of.

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #25

                            @JNBarchan
                            Yeah that makes sense. I was thinking in terms of the Qt Class but of course child has to keep a copy.

                            Ok so at least it is a bit locked down :)

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              mchinand
                              wrote on last edited by
                              #26

                              Another option that I think will work (and more secure) is to use the command-line option "-p" alone without the password. In this case, it normally prompts the user for the password. You should be able to do a

                              process->write("yourpassword\n");
                              

                              after starting the process. I'm not sure if you'll need to add a short delay between start() and this write or not. No temporary file containing the password, no password on the command-line, no password in environment variables.

                              JonBJ 2 Replies Last reply
                              1
                              • M mchinand

                                Another option that I think will work (and more secure) is to use the command-line option "-p" alone without the password. In this case, it normally prompts the user for the password. You should be able to do a

                                process->write("yourpassword\n");
                                

                                after starting the process. I'm not sure if you'll need to add a short delay between start() and this write or not. No temporary file containing the password, no password on the command-line, no password in environment variables.

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

                                @mchinand
                                Ah, now that is interesting indeed. I shall give it a try and report back....

                                1 Reply Last reply
                                0
                                • M mchinand

                                  Another option that I think will work (and more secure) is to use the command-line option "-p" alone without the password. In this case, it normally prompts the user for the password. You should be able to do a

                                  process->write("yourpassword\n");
                                  

                                  after starting the process. I'm not sure if you'll need to add a short delay between start() and this write or not. No temporary file containing the password, no password on the command-line, no password in environment variables.

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

                                  @mchinand , and others

                                  Firstly, @mchinand your QProcess:write(password) does indeed work, just wanted to say that.

                                  After weighing up --password command-line, --defaults-file temporary-file command-line, ~/mylogin.cnf (with & without encryption) and QProcess:write, I have actually finally come down on the side of @mchinand's MYSQL_PWD environment variable suggestion. They all have pros & cons, but for various reasons (which I'll explain only if you're truly interested!) the environment variable passing suits my code the best and is simplest.

                                  For anyone who cares, my pattern is now:

                                  self.process = QProcess()
                                  env = self.process.processEnvironment()
                                  env.insert("MYSQL_PWD", password)
                                  self.process.setProcessEnvironment(env)
                                  self.process.start("{} {}".format(program, args))
                                  

                                  Note how the way it works from Qt is that you don't put the environment variable into parent process's own environment for inheritance to child, you put it into QProcess's environment for the child process only. Which suits me --- no need to remove it from my own environment after starting sub-process.

                                  1 Reply Last reply
                                  3

                                  • Login

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