Re: [fpc-pascal] GetTempFileName in Linux
Am 05.10.2010 17:41, schrieb Jonas Maebe: On 05 Oct 2010, at 17:38, Michael Van Canneyt wrote: On Tue, 5 Oct 2010, Leonardo M. Ramé wrote: Hi, if I run this program from command line in Linux, I allways get the same result, /tmp/TMP0.tmp. Shouldn't it return a different file name each time it's executed?. How can I get different file names? By actually creating the file. GetTempFileName does not create a file, but does check whether the filename exists. What is the use of that? Between the check and creating the file, some other process may already have created it. That might be the reason why Windows is creating the file for you if you call GetTempFileName... so GetTempFileName in FPC does not expose the same behavior on non-Windows and Windows. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Re: TProcess.Free - three exceptions in ntdll.dll - only *sometimes*
Am 05.10.2010 21:08, schrieb Bernd Kreuss: On 05.10.2010 20:55, Bernd Kreuss wrote: I see this error message now exactly once, immediately *after* the DLL has been unloaded by the host application. Correction: I see it after my log output within my finalization section, it might still be in the final stages of DLL_PROCESS_DETACH. Where exactly is the finalization (the finalization section in my source code) hooked into? is it Dll_Process_Detach_Hook? If I remember correctly, yes. Search for fpc_do_exit in the rtl/ directory (and the methods called from there). On a sidenote: The critical section should not be necessary, because DllMain is called serialized by the OS (according to http://blogs.msdn.com/b/oldnewthing/archive/2004/01/28/63880.aspx ). Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetTempFileName in Linux
Am 05.10.2010 18:26, schrieb ik: On Tue, Oct 5, 2010 at 17:30, Sven Barth pascaldra...@googlemail.com mailto:pascaldra...@googlemail.com wrote: Am 05.10.2010 17:20, schrieb Leonardo M. Ramé: Hi, if I run this program from command line in Linux, I allways get the same result, /tmp/TMP0.tmp. Shouldn't it return a different file name each time it's executed?. How can I get different file names? program tempfilename; uses sysutils; begin writeln(GetTempFileName); end. Did you delete the file after the run of your program? GetTempFileName always starts from 0 and checks whether that file already exists. If not, it returns that name else it continues increasing the index. That's a security risk, because it is very easy to know what will be the file name. It should return random name that does not exists on a random length (that the developer chooses). Symlink attack: http://www.infosecwriters.com/texts.php?op=displayid=159 http://www.infosecwriters.com/texts.php?op=displayid=159 for better understanding. Windows (or at least ReactOS) seems to use GetTickCount to generate a random filename: http://svn.reactos.org/svn/reactos/trunk/reactos/dll/win32/kernel32/file/file.c?revision=48845view=markup (look for GetTempFilenameW) Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetTempFileName in Linux
On Wed, 6 Oct 2010, Sven Barth wrote: Am 05.10.2010 17:41, schrieb Jonas Maebe: On 05 Oct 2010, at 17:38, Michael Van Canneyt wrote: On Tue, 5 Oct 2010, Leonardo M. Ramé wrote: Hi, if I run this program from command line in Linux, I allways get the same result, /tmp/TMP0.tmp. Shouldn't it return a different file name each time it's executed?. How can I get different file names? By actually creating the file. GetTempFileName does not create a file, but does check whether the filename exists. What is the use of that? Between the check and creating the file, some other process may already have created it. This is always true on Unix, the only way to make sure is to have the kernel create the temporary name and file for you. Unix - to my knowledge - does not have a way to create and lock a file in one atomic operation; There are always 2 operations involved, and so anything can happen between the 2 calls. That might be the reason why Windows is creating the file for you if you call GetTempFileName... so GetTempFileName in FPC does not expose the same behavior on non-Windows and Windows. I'm open for some more sophisticated algorithm which does some more checking. Michael.___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetTempFileName in Linux
On 06 Oct 2010, at 09:41, Michael Van Canneyt wrote: This is always true on Unix, the only way to make sure is to have the kernel create the temporary name and file for you. Unix - to my knowledge - does not have a way to create and lock a file in one atomic operation; There are always 2 operations involved, and so anything can happen between the 2 calls. You can at least open a file with O_CREAT|O_EXCL|O_NOFOLLOW to make sure that it does not yet exist at the point that you create it (and that it's not a symlink either). Jonas ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetTempFileName in Linux
On Wed, 6 Oct 2010, Jonas Maebe wrote: On 06 Oct 2010, at 09:41, Michael Van Canneyt wrote: This is always true on Unix, the only way to make sure is to have the kernel create the temporary name and file for you. Unix - to my knowledge - does not have a way to create and lock a file in one atomic operation; There are always 2 operations involved, and so anything can happen between the 2 calls. You can at least open a file with O_CREAT|O_EXCL|O_NOFOLLOW to make sure that it does not yet exist at the point that you create it (and that it's not a symlink either). This is correct, but doesn't lock the file, and so it doesn't prevent someone from 'stealing' the file before the lock is applied. It's of course a remote possibility. Personally, I have more problems with windows which actually creates the files and locks them. It sometimes requires a reboot to release the lock again in case something happens to the calling process. That said, I repeat: no problem if someone implements a more secure algorithm. Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetTempFileName in Linux
On 06 Oct 2010, at 11:05, Michael Van Canneyt wrote: On Wed, 6 Oct 2010, Jonas Maebe wrote: On 06 Oct 2010, at 09:41, Michael Van Canneyt wrote: This is always true on Unix, the only way to make sure is to have the kernel create the temporary name and file for you. Unix - to my knowledge - does not have a way to create and lock a file in one atomic operation; There are always 2 operations involved, and so anything can happen between the 2 calls. You can at least open a file with O_CREAT|O_EXCL|O_NOFOLLOW to make sure that it does not yet exist at the point that you create it (and that it's not a symlink either). This is correct, but doesn't lock the file, Locking is always advisory on Unix, so that doesn't matter anyway. and so it doesn't prevent someone from 'stealing' the file before the lock is applied. Nobody else can steal the file once you have created it, because they won't be the owner nor have the necessary permissions. That is the main security risk and it is solved by this approach. The fact that another process running under your login not using O_EXCL could overwrite it is not an extra security risk (if you have a rogue process running under your login, nothing that you do is safe because it can use ptrace to modify any process in any way it sees fit anyway). Jonas ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetTempFileName in Linux
On Wed, 6 Oct 2010, Jonas Maebe wrote: On 06 Oct 2010, at 11:05, Michael Van Canneyt wrote: On Wed, 6 Oct 2010, Jonas Maebe wrote: On 06 Oct 2010, at 09:41, Michael Van Canneyt wrote: This is always true on Unix, the only way to make sure is to have the kernel create the temporary name and file for you. Unix - to my knowledge - does not have a way to create and lock a file in one atomic operation; There are always 2 operations involved, and so anything can happen between the 2 calls. You can at least open a file with O_CREAT|O_EXCL|O_NOFOLLOW to make sure that it does not yet exist at the point that you create it (and that it's not a symlink either). This is correct, but doesn't lock the file, Locking is always advisory on Unix, so that doesn't matter anyway. and so it doesn't prevent someone from 'stealing' the file before the lock is applied. Nobody else can steal the file once you have created it, because they won't be the owner nor have the necessary permissions. That is the main security risk and it is solved by this approach. The fact that another process running under your login not using O_EXCL could overwrite it is not an extra security risk (if you have a rogue process running under your login, nothing that you do is safe because it can use ptrace to modify any process in any way it sees fit anyway). And that is why I think the whole point is hugely exaggerated :-) But it doesn't mean we shouldn't do our best to make it minimally safe. Michael. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetTempFileName in Linux
On 06 Oct 2010, at 11:59, Michael Van Canneyt wrote: On Wed, 6 Oct 2010, Jonas Maebe wrote: Nobody else can steal the file once you have created it, because they won't be the owner nor have the necessary permissions. That is the main security risk and it is solved by this approach. The fact that another process running under your login not using O_EXCL could overwrite it is not an extra security risk (if you have a rogue process running under your login, nothing that you do is safe because it can use ptrace to modify any process in any way it sees fit anyway). And that is why I think the whole point is hugely exaggerated :-) It's not, because currently a process from *another* user can trivially intercept your temporary files. If you have a daemon running as root, that is a major security concern. Jonas ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] lNet getting the local IP
Am 06.10.2010 14:49, schrieb Andrew Brunner: I just read the first line in /etc/hosts file. The first entry is the IPv4. On Tue, Oct 5, 2010 at 3:04 PM, Felipe Monteiro de Carvalho felipemonteiro.carva...@gmail.com wrote: hello, I am searching for a way to get the local IP. I already found examples with winsock, synapse and indy. Does anyone know how to do that with lNet? thanks, Huh?! O.o You'll only find 127.0.0.1 for my computer there... I don't think that this is a very good solution. Regards, Sven ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] lNet getting the local IP
Ok, thanks everyone, it seams that I managed to extract a function from Silvio's code which doesn't use Synapse. I only tested in Windows so far: unit chesstcputils; {$mode objfpc}{$H+} interface uses {$IFDEF MSWINDOWS} Winsock, {$ENDIF} Classes, SysUtils; function ChessGetLocalIP(): string; implementation const CFormatIPMask = '%d.%d.%d.%d'; function ChessGetLocalIP(): string; var I, VAttempt: Integer; VStrTemp, VSitesToTry: TStringList; {$IFDEF UNIX} VProcess: TProcess; {$ENDIF} {$IFDEF MSWINDOWS} var VWSAData: TWSAData; VHostEnt: PHostEnt; VName: string; {$ENDIF} begin Result := ''; {$IFDEF UNIX} VStrTemp := TStringList.Create; VProcess := TProcess.Create(nil); try VProcess.CommandLine := 'sh -c ifconfig eth0 | awk ''/inet end/ {print $3}'''; VProcess.Options := [poWaitOnExit, poUsePipes]; VProcess.Execute; VStrTemp.LoadFromStream(VProcess.Output); Result := Trim(VStrTemp.Text); finally VStrTemp.Free; VProcess.Free; end; {$ENDIF} {$IFDEF MSWINDOWS} {$HINTS OFF} WSAStartup(2, VWSAData); {$HINTS ON} SetLength(VName, 255); GetHostName(PChar(VName), 255); SetLength(VName, StrLen(PChar(VName))); VHostEnt := GetHostByName(PChar(VName)); with VHostEnt^ do Result := Format(CFormatIPMask, [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]); WSACleanup; {$ENDIF} end; end. -- Felipe Monteiro de Carvalho ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] lNet getting the local IP
On Wed, Oct 6, 2010 at 7:55 AM, Sven Barth pascaldra...@googlemail.com wrote: You'll only find 127.0.0.1 for my computer there... I don't think that this is a very good solution. Oh well. That makes sense b/c these are servers that I have obtaining the same IP over DHCP. On clients when DHCP is being used with dynamic IPs the host file may not be of much use or may require more parsing for the host name. But from a file based solution you just need to read in the /etc/hostname file. That is the name of the box. With the host name, ust search the /etc/hosts file for the IP matching that name. It's similar to the ifconfig command with parsing of eth0 which by the way if there are more than 1 nick, could be problematic. Is there an Gnome or KDE API we could use to officially get NIC related info like iphlpapi.dll API under Windows? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] lNet getting the local IP
In our previous episode, Felipe Monteiro de Carvalho said: function ChessGetLocalIP(): string; VProcess.CommandLine := 'sh -c ifconfig eth0 | awk ''/inet end/ {print $3}'''; Fails if eth0 is not the primary NIC. Doesn't work on non-linux (that use other ethernet naming schemes) A safe solution will probably boil down to walking the list of ethernet adapters and routes to find the default route on *nix. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] lNet getting the local IP
On 06/10/10 14:27, Felipe Monteiro de Carvalho wrote: Ok, thanks everyone, it seams that I managed to extract a function from Silvio's code which doesn't use Synapse. I only tested in Windows so far: unit chesstcputils; {$mode objfpc}{$H+} interface uses {$IFDEF MSWINDOWS} Winsock, {$ENDIF} Classes, SysUtils; function ChessGetLocalIP(): string; implementation const CFormatIPMask = '%d.%d.%d.%d'; function ChessGetLocalIP(): string; var I, VAttempt: Integer; VStrTemp, VSitesToTry: TStringList; {$IFDEF UNIX} VProcess: TProcess; {$ENDIF} {$IFDEF MSWINDOWS} var VWSAData: TWSAData; VHostEnt: PHostEnt; VName: string; {$ENDIF} begin Result := ''; {$IFDEF UNIX} VStrTemp := TStringList.Create; VProcess := TProcess.Create(nil); try VProcess.CommandLine := 'sh -c ifconfig eth0 | awk ''/inet end/ {print $3}'''; Yuck. This doesn't work on my system (debian). If you really want the least effort, you may have more luck with simply parsing `hostname -I` somehow. The right way to do this is with an ioctl, I believe (SIOCGIFCONF). Look here: http://www.kernel.org/doc/man-pages/online/pages/man7/netdevice.7.html I'm sure there's some code floating around, but it probably means that you have to translate some headers :(. Henry ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] GetTempFileName in Linux
On Wed, 6 Oct 2010 09:41:03 +0200 (CEST) Michael Van Canneyt mich...@freepascal.org wrote: I'm open for some more sophisticated algorithm which does some more checking. Michael. Honour one of the temp directory environment variables, I know of TMPDIR, TEMP, TMP. I think it would be best to check them in order and then default to /tmp/. Create a folder in the temp directory naming it after the user name. E.g /tmp/myusername/ Ensure only the owner can read and write in the folder. Create a folder in /tmp/myusername/ using your program name. Then create your temp file. so you have /tmp/myusername/programname/tmp01 You should only need to check for symlinks, hardlinks and read/write rights on the /tmp/myusername folder. What is the cost? Two more inodes More cpu time What do you get? It hides the file's names Good layout Easier to use Tomoyo and Apparmour. On Wed, 06 Oct 2010 09:40:42 +0200 Sven Barth pascaldra...@googlemail.com wrote: Windows (or at least ReactOS) seems to use GetTickCount to generate a random filename: Do not you use random file names without a subdirectory because you can not use Tomoyo or Apparmour to lock the program down. I use this trick with Firefox. From my .bashrc file mkdir /tmp/$USER /dev/null 21 chmod u=rwx,g=rwx,o-rwx /tmp/$USER /dev/null 21 export TMPDIR=/tmp/$USER /dev/null 21 #!/bin/sh mkdir $TMPDIR/firefox/ env TMPDIR=$TMPDIR/firefox /usr/bin/firefox $@ From my Tomoyo profile: allow_read/write /tmp/\*/firefox/\* allow_rename /tmp/\*/firefox/\* /tmp/\*/firefox/\* allow_rename /tmp/\*/firefox/\*.part /home/\*/Downloads/\* allow_truncate /tmp/\*/firefox/\* allow_unlink /tmp/\*/firefox/\* Since there is no way to predict what the file name will be, permissions have to be done on a per folder basis, not per file. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] lNet getting the local IP
On Wed, Oct 6, 2010 at 7:09 AM, fpc-pascal-requ...@lists.freepascal.org wrote: Send fpc-pascal mailing list submissions to fpc-pas...@lists.freepascal.org To subscribe or unsubscribe via the World Wide Web, visit http://lists.freepascal.org/mailman/listinfo/fpc-pascal or, via email, send a message with subject or body 'help' to fpc-pascal-requ...@lists.freepascal.org You can reach the person managing the list at fpc-pascal-ow...@lists.freepascal.org When replying, please edit your Subject line so it is more specific than Re: Contents of fpc-pascal digest... Today's Topics: 1. Re: GetTempFileName in Linux (Michael Van Canneyt) 2. Re: GetTempFileName in Linux (Jonas Maebe) 3. Re: lNet getting the local IP (Andrew Brunner) 4. Re: lNet getting the local IP (Sven Barth) 5. Re: lNet getting the local IP (Felipe Monteiro de Carvalho) 6. Re: lNet getting the local IP (Andrew Brunner) 7. Re: lNet getting the local IP (Marco van de Voort) 8. Re: lNet getting the local IP (Henry Vermaak) -- Message: 1 Date: Wed, 6 Oct 2010 11:59:53 +0200 (CEST) From: Michael Van Canneyt mich...@freepascal.org Subject: Re: [fpc-pascal] GetTempFileName in Linux To: FPC-Pascal users discussions fpc-pascal@lists.freepascal.org Message-ID: alpine.deb.2.00.1010061158450.6...@fsb.wals.be Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed On Wed, 6 Oct 2010, Jonas Maebe wrote: On 06 Oct 2010, at 11:05, Michael Van Canneyt wrote: On Wed, 6 Oct 2010, Jonas Maebe wrote: On 06 Oct 2010, at 09:41, Michael Van Canneyt wrote: This is always true on Unix, the only way to make sure is to have the kernel create the temporary name and file for you. Unix - to my knowledge - does not have a way to create and lock a file in one atomic operation; There are always 2 operations involved, and so anything can happen between the 2 calls. You can at least open a file with O_CREAT|O_EXCL|O_NOFOLLOW to make sure that it does not yet exist at the point that you create it (and that it's not a symlink either). This is correct, but doesn't lock the file, Locking is always advisory on Unix, so that doesn't matter anyway. and so it doesn't prevent someone from 'stealing' the file before the lock is applied. Nobody else can steal the file once you have created it, because they won't be the owner nor have the necessary permissions. That is the main security risk and it is solved by this approach. The fact that another process running under your login not using O_EXCL could overwrite it is not an extra security risk (if you have a rogue process running under your login, nothing that you do is safe because it can use ptrace to modify any process in any way it sees fit anyway). And that is why I think the whole point is hugely exaggerated :-) But it doesn't mean we shouldn't do our best to make it minimally safe. Michael. -- Message: 2 Date: Wed, 6 Oct 2010 12:57:02 +0200 From: Jonas Maebe jonas.ma...@elis.ugent.be Subject: Re: [fpc-pascal] GetTempFileName in Linux To: FPC-Pascal users discussions fpc-pascal@lists.freepascal.org Message-ID: 6cd7d25a-fd10-4c7d-9011-da47560af...@elis.ugent.be Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes On 06 Oct 2010, at 11:59, Michael Van Canneyt wrote: On Wed, 6 Oct 2010, Jonas Maebe wrote: Nobody else can steal the file once you have created it, because they won't be the owner nor have the necessary permissions. That is the main security risk and it is solved by this approach. The fact that another process running under your login not using O_EXCL could overwrite it is not an extra security risk (if you have a rogue process running under your login, nothing that you do is safe because it can use ptrace to modify any process in any way it sees fit anyway). And that is why I think the whole point is hugely exaggerated :-) It's not, because currently a process from *another* user can trivially intercept your temporary files. If you have a daemon running as root, that is a major security concern. Jonas -- Message: 3 Date: Wed, 6 Oct 2010 07:49:42 -0500 From: Andrew Brunner andrew.t.brun...@gmail.com Subject: Re: [fpc-pascal] lNet getting the local IP To: FPC-Pascal users discussions fpc-pascal@lists.freepascal.org Message-ID: aanlktinkfgmjmfkls=tww+sorr3jmkj4zjul7z6zr...@mail.gmail.com Content-Type: text/plain; charset=ISO-8859-1 I just read the first line in /etc/hosts file. The first entry is the IPv4. On Tue, Oct 5, 2010 at 3:04 PM, Felipe Monteiro de Carvalho felipemonteiro.carva...@gmail.com wrote: hello, I am searching for a way to get the local IP. I already found examples with winsock, synapse and indy. Does anyone know how to do that with lNet? thanks, -- Felipe Monteiro de Carvalho