Is there a file that always exists and a 'normal' user can't lstat it?Make all files under a directory...

How to politely refuse in-office gym instructor for steroids and protein

How do I narratively explain how in-game circumstances do not mechanically allow a PC to instantly kill an NPC?

Is it possible to rotate the Isolines on a Surface Using `MeshFunction`?

How do I add a strong "onion flavor" to the biryani (in restaurant style)?

What to do with threats of blacklisting?

How do you get out of your own psychology to write characters?

Why didn't Tom Riddle take the presence of Fawkes and the Sorting Hat as more of a threat?

Taking an academic pseudonym?

How do dictionaries source attestation?

How long has this character been impersonating a Starfleet Officer?

Is the fingering of thirds flexible or do I have to follow the rules?

Illustrator to chemdraw

How bad is a Computer Science course that doesn't teach Design Patterns?

If I tried and failed to start my own business, how do I apply for a job without job experience?

Buying a "Used" Router

Minimum Viable Product for RTS game?

Was there a pre-determined arrangement for the division of Germany in case it surrendered before any Soviet forces entered its territory?

Charging phone battery with a lower voltage, coming from a bike charger?

I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."

Why do neural networks need so many examples to perform?

Is there any danger of my neighbor having my wife's signature?

Allow console draw poker game to output more hands

Prevent Nautilus / Nemo from creating .Trash-1000 folder in mounted devices

Crack the bank account's password!



Is there a file that always exists and a 'normal' user can't lstat it?


Make all files under a directory read-only without changing permissions?Setting up a shared folder with group: ACL and file creationMonitor file access count by userChanging user to root when connected to a linux server and copying filesHow to remount as read-write a specific mount of device?Linux user groups and file system permissionsSyslog config syntaxAccess to the internet from a firewalled serverhwclock can't open rtc fileGive read permission to non-root user for root owned file?













4















I need this for a unit test. There's a function, which does lstat on the file path passed in its parameter. I have to trigger the code path when the lstat fails. (Because the code coverage has to reach 90%)



The test can run only under a single user, therefore I was thinking if there's a file in Ubuntu, which always exists but normal users have no read access to it, or its folder. (So lstat would fail on it unless executed as root.)



Non-existing file is not a solution, because there's a separate code path for that, which I'm already triggering.



EDIT: Lack of read access to the file only is not enough. With that lstat can still be executed. I was able to trigger it (on my local machine, where I have root access), by creating a folder in /root, and a file in it. And setting permission 700 on the folder. So I'm searching for a file, which is in a folder that is only accessible by root.










share|improve this question




















  • 1





    IMHO /etc/shadow

    – Romeo Ninov
    1 hour ago











  • @RomeoNinov thanks. Maybe my question is wrong. That file is readable, but I can lstat it. Then its directory has to be non-readable?

    – Crouching Kitten
    58 mins ago






  • 1





    You cannot assume the existence of any file, because your program may run in a chroot or separate namespace. If assuming that /proc is mounted is OK and init is nothing special, then /proc/1/fd/0 should do.

    – mosvy
    43 mins ago








  • 1





    @mosvy Thanks that works on my local machine. Hmm then I'll try it on the QA and Staging pool too.

    – Crouching Kitten
    40 mins ago
















4















I need this for a unit test. There's a function, which does lstat on the file path passed in its parameter. I have to trigger the code path when the lstat fails. (Because the code coverage has to reach 90%)



The test can run only under a single user, therefore I was thinking if there's a file in Ubuntu, which always exists but normal users have no read access to it, or its folder. (So lstat would fail on it unless executed as root.)



Non-existing file is not a solution, because there's a separate code path for that, which I'm already triggering.



EDIT: Lack of read access to the file only is not enough. With that lstat can still be executed. I was able to trigger it (on my local machine, where I have root access), by creating a folder in /root, and a file in it. And setting permission 700 on the folder. So I'm searching for a file, which is in a folder that is only accessible by root.










share|improve this question




















  • 1





    IMHO /etc/shadow

    – Romeo Ninov
    1 hour ago











  • @RomeoNinov thanks. Maybe my question is wrong. That file is readable, but I can lstat it. Then its directory has to be non-readable?

    – Crouching Kitten
    58 mins ago






  • 1





    You cannot assume the existence of any file, because your program may run in a chroot or separate namespace. If assuming that /proc is mounted is OK and init is nothing special, then /proc/1/fd/0 should do.

    – mosvy
    43 mins ago








  • 1





    @mosvy Thanks that works on my local machine. Hmm then I'll try it on the QA and Staging pool too.

    – Crouching Kitten
    40 mins ago














4












4








4








I need this for a unit test. There's a function, which does lstat on the file path passed in its parameter. I have to trigger the code path when the lstat fails. (Because the code coverage has to reach 90%)



The test can run only under a single user, therefore I was thinking if there's a file in Ubuntu, which always exists but normal users have no read access to it, or its folder. (So lstat would fail on it unless executed as root.)



Non-existing file is not a solution, because there's a separate code path for that, which I'm already triggering.



EDIT: Lack of read access to the file only is not enough. With that lstat can still be executed. I was able to trigger it (on my local machine, where I have root access), by creating a folder in /root, and a file in it. And setting permission 700 on the folder. So I'm searching for a file, which is in a folder that is only accessible by root.










share|improve this question
















I need this for a unit test. There's a function, which does lstat on the file path passed in its parameter. I have to trigger the code path when the lstat fails. (Because the code coverage has to reach 90%)



The test can run only under a single user, therefore I was thinking if there's a file in Ubuntu, which always exists but normal users have no read access to it, or its folder. (So lstat would fail on it unless executed as root.)



Non-existing file is not a solution, because there's a separate code path for that, which I'm already triggering.



EDIT: Lack of read access to the file only is not enough. With that lstat can still be executed. I was able to trigger it (on my local machine, where I have root access), by creating a folder in /root, and a file in it. And setting permission 700 on the folder. So I'm searching for a file, which is in a folder that is only accessible by root.







linux ubuntu






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 48 mins ago







Crouching Kitten

















asked 1 hour ago









Crouching KittenCrouching Kitten

273310




273310








  • 1





    IMHO /etc/shadow

    – Romeo Ninov
    1 hour ago











  • @RomeoNinov thanks. Maybe my question is wrong. That file is readable, but I can lstat it. Then its directory has to be non-readable?

    – Crouching Kitten
    58 mins ago






  • 1





    You cannot assume the existence of any file, because your program may run in a chroot or separate namespace. If assuming that /proc is mounted is OK and init is nothing special, then /proc/1/fd/0 should do.

    – mosvy
    43 mins ago








  • 1





    @mosvy Thanks that works on my local machine. Hmm then I'll try it on the QA and Staging pool too.

    – Crouching Kitten
    40 mins ago














  • 1





    IMHO /etc/shadow

    – Romeo Ninov
    1 hour ago











  • @RomeoNinov thanks. Maybe my question is wrong. That file is readable, but I can lstat it. Then its directory has to be non-readable?

    – Crouching Kitten
    58 mins ago






  • 1





    You cannot assume the existence of any file, because your program may run in a chroot or separate namespace. If assuming that /proc is mounted is OK and init is nothing special, then /proc/1/fd/0 should do.

    – mosvy
    43 mins ago








  • 1





    @mosvy Thanks that works on my local machine. Hmm then I'll try it on the QA and Staging pool too.

    – Crouching Kitten
    40 mins ago








1




1





IMHO /etc/shadow

– Romeo Ninov
1 hour ago





IMHO /etc/shadow

– Romeo Ninov
1 hour ago













@RomeoNinov thanks. Maybe my question is wrong. That file is readable, but I can lstat it. Then its directory has to be non-readable?

– Crouching Kitten
58 mins ago





@RomeoNinov thanks. Maybe my question is wrong. That file is readable, but I can lstat it. Then its directory has to be non-readable?

– Crouching Kitten
58 mins ago




1




1





You cannot assume the existence of any file, because your program may run in a chroot or separate namespace. If assuming that /proc is mounted is OK and init is nothing special, then /proc/1/fd/0 should do.

– mosvy
43 mins ago







You cannot assume the existence of any file, because your program may run in a chroot or separate namespace. If assuming that /proc is mounted is OK and init is nothing special, then /proc/1/fd/0 should do.

– mosvy
43 mins ago






1




1





@mosvy Thanks that works on my local machine. Hmm then I'll try it on the QA and Staging pool too.

– Crouching Kitten
40 mins ago





@mosvy Thanks that works on my local machine. Hmm then I'll try it on the QA and Staging pool too.

– Crouching Kitten
40 mins ago










3 Answers
3






active

oldest

votes


















3














On modern Linux systems, you should be able to use /proc/1/fdinfo/0 (information for the file descriptor 1 (stdout) of the process of id 1 (init in the root pid namespace which should be running as root)).



You can find a list with (zsh or bash syntax):



sudo find /etc /dev /sys /proc -type f -print0 |
while IFS= read -rd '' f; do
[ -e "$f" ] || printf '%sn' "$f"
done


(remove -type f if you don't want to restrict to regular files, but then you'd also need to change [ -e "$f" ] to [ -e "$f" ] || [ -L "$f" ] or use zsh's stat -L builtin instead of [).






share|improve this answer





















  • 1





    Thanks! If /proc/1/fdinfo/0 works on Ubuntu 16.04 and 18.04, that's more than enough.

    – Crouching Kitten
    37 mins ago



















4














You can find it yourself.



Using /etc -- the configuration files directory as a starting point:



sudo find /etc -type f -perm 0400 -user root


On my system, this does not return anything.



You can be a less restrictive and allow group root (only user root should be a member of group root), and a look out for a permission of 440:



sudo find /etc -perm 0440 -user root -group root


On my system this returns:



/etc/sudoers.d/README
/etc/sudoers




Edit:



Based on your edit, you're looking for a directory that does not have sufficient permission for the invoking user to prevent directory listing:



sudo find / -perm o-rwx -user root -group root 


here i'm looking for directories (type d) that lacks the read-write-execute perm bits for others (o-rwx) and is owned by root:root.



Technically, just the absense of execute (x) bit would prevent a directory listing (lstat(2)) on directory.



In the output i've found /run/systemd/inaccessible/ on my Systemd init based system.



Regarding files in /proc, /sys, /dev:




  • These filesystems are virtual FS i.e. they reside on memory, not on disk


  • If you plan to rely on /proc, use /proc/1/ i.e. rely on something under PID 1, not any later PIDs to have reliability/consistency as the later PIDs (processes) are not guranteed to exist.







share|improve this answer


























  • Thanks, I think my question is wrong. I can still lstat files without read access to them. Maybe the access to the folder has to be limited? (I modified the title)

    – Crouching Kitten
    55 mins ago













  • Thanks. With find / -type d -perm 0400 -user root I have found the directory /proc/20/map_files/, if I refer to a made-up file name inside that folder, like /proc/20/map_files/asdasd, then it always fails. Does that folder always exist on Ubuntu?

    – Crouching Kitten
    43 mins ago











  • @CrouchingKitten, the directories in /proc/1/ might be safer, since init always exists. But that's proc, not a regular filesystem, in case it matters.

    – ilkkachu
    37 mins ago











  • Thanks I gave an upvote, but accepted the other answer, because he said it is guaranteed that /proc/1/fdinfo/0 works on modern Ubuntus.

    – Crouching Kitten
    34 mins ago













  • It's the absence of x that prevents the lstat() of files in it, not r.

    – Stéphane Chazelas
    22 mins ago





















3














Looking at the lstat(2) man page you can get some inspiration on cases that might make it fail with errors other than ENOENT (file does not exist.)



The most obvious one is:




EACCES
Search permission is denied for one of the directories in the path prefix of path.




So you need a directory you can't read from.



Yes, you can look for one that's already in your system (perhaps /var/lib/private if it exists?) But you might as well create one yourself, with the equivalent of:



$ mkdir myprivatedir
$ touch myprivatedir/myunreachablefile
$ chmod 0 myprivatedir
$ ls -l myprivatedir/myunreachablefile


The lstat(2) operation will fail with EACCES here. (Removing all permissions from the directory ensures that. Maybe you don't even need that much and chmod -x removing execute permissions would be enough, since execute permissions on a directory are needed to access files under it.)



There's another creative way to make lstat(2) fail, looking at its man page:




ENOTDIR
A component of the path prefix of path is not a directory.




So, trying to access a file such as /etc/passwd/nonexistent should trigger this error, which again is different from ENOENT ("No such file or directory") and might suit your needs.



Another one is:




ENAMETOOLONG
path is too long.




But you might need a really long name for this one (I believe 4,096 bytes is the typical limit, but your system/filesystem might have a longer one.)



Finally, it's hard to tell whether any of these will be actually useful for you. You say you want something that doesn't trigger the "file doesn't exist" scenario. While typically that means an ENOENT error, in practice many higher-level checks will simply interpret any errors from lstat(2) as "does not exist". For example test -e or the equivalent [ -e ...] from the shell might simply just interpret all of the above as "does not exist", especially since it doesn't have a good way to return a different error message and not returning an error would imply the file exists, which is most certainly not the case.






share|improve this answer
























  • It's not directories you can't read from, but directories you can't search (for which you don't have the search (x) permission, or to one of its parents).

    – Stéphane Chazelas
    18 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502857%2fis-there-a-file-that-always-exists-and-a-normal-user-cant-lstat-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














On modern Linux systems, you should be able to use /proc/1/fdinfo/0 (information for the file descriptor 1 (stdout) of the process of id 1 (init in the root pid namespace which should be running as root)).



You can find a list with (zsh or bash syntax):



sudo find /etc /dev /sys /proc -type f -print0 |
while IFS= read -rd '' f; do
[ -e "$f" ] || printf '%sn' "$f"
done


(remove -type f if you don't want to restrict to regular files, but then you'd also need to change [ -e "$f" ] to [ -e "$f" ] || [ -L "$f" ] or use zsh's stat -L builtin instead of [).






share|improve this answer





















  • 1





    Thanks! If /proc/1/fdinfo/0 works on Ubuntu 16.04 and 18.04, that's more than enough.

    – Crouching Kitten
    37 mins ago
















3














On modern Linux systems, you should be able to use /proc/1/fdinfo/0 (information for the file descriptor 1 (stdout) of the process of id 1 (init in the root pid namespace which should be running as root)).



You can find a list with (zsh or bash syntax):



sudo find /etc /dev /sys /proc -type f -print0 |
while IFS= read -rd '' f; do
[ -e "$f" ] || printf '%sn' "$f"
done


(remove -type f if you don't want to restrict to regular files, but then you'd also need to change [ -e "$f" ] to [ -e "$f" ] || [ -L "$f" ] or use zsh's stat -L builtin instead of [).






share|improve this answer





















  • 1





    Thanks! If /proc/1/fdinfo/0 works on Ubuntu 16.04 and 18.04, that's more than enough.

    – Crouching Kitten
    37 mins ago














3












3








3







On modern Linux systems, you should be able to use /proc/1/fdinfo/0 (information for the file descriptor 1 (stdout) of the process of id 1 (init in the root pid namespace which should be running as root)).



You can find a list with (zsh or bash syntax):



sudo find /etc /dev /sys /proc -type f -print0 |
while IFS= read -rd '' f; do
[ -e "$f" ] || printf '%sn' "$f"
done


(remove -type f if you don't want to restrict to regular files, but then you'd also need to change [ -e "$f" ] to [ -e "$f" ] || [ -L "$f" ] or use zsh's stat -L builtin instead of [).






share|improve this answer















On modern Linux systems, you should be able to use /proc/1/fdinfo/0 (information for the file descriptor 1 (stdout) of the process of id 1 (init in the root pid namespace which should be running as root)).



You can find a list with (zsh or bash syntax):



sudo find /etc /dev /sys /proc -type f -print0 |
while IFS= read -rd '' f; do
[ -e "$f" ] || printf '%sn' "$f"
done


(remove -type f if you don't want to restrict to regular files, but then you'd also need to change [ -e "$f" ] to [ -e "$f" ] || [ -L "$f" ] or use zsh's stat -L builtin instead of [).







share|improve this answer














share|improve this answer



share|improve this answer








edited 37 mins ago

























answered 40 mins ago









Stéphane ChazelasStéphane Chazelas

307k57581936




307k57581936








  • 1





    Thanks! If /proc/1/fdinfo/0 works on Ubuntu 16.04 and 18.04, that's more than enough.

    – Crouching Kitten
    37 mins ago














  • 1





    Thanks! If /proc/1/fdinfo/0 works on Ubuntu 16.04 and 18.04, that's more than enough.

    – Crouching Kitten
    37 mins ago








1




1





Thanks! If /proc/1/fdinfo/0 works on Ubuntu 16.04 and 18.04, that's more than enough.

– Crouching Kitten
37 mins ago





Thanks! If /proc/1/fdinfo/0 works on Ubuntu 16.04 and 18.04, that's more than enough.

– Crouching Kitten
37 mins ago













4














You can find it yourself.



Using /etc -- the configuration files directory as a starting point:



sudo find /etc -type f -perm 0400 -user root


On my system, this does not return anything.



You can be a less restrictive and allow group root (only user root should be a member of group root), and a look out for a permission of 440:



sudo find /etc -perm 0440 -user root -group root


On my system this returns:



/etc/sudoers.d/README
/etc/sudoers




Edit:



Based on your edit, you're looking for a directory that does not have sufficient permission for the invoking user to prevent directory listing:



sudo find / -perm o-rwx -user root -group root 


here i'm looking for directories (type d) that lacks the read-write-execute perm bits for others (o-rwx) and is owned by root:root.



Technically, just the absense of execute (x) bit would prevent a directory listing (lstat(2)) on directory.



In the output i've found /run/systemd/inaccessible/ on my Systemd init based system.



Regarding files in /proc, /sys, /dev:




  • These filesystems are virtual FS i.e. they reside on memory, not on disk


  • If you plan to rely on /proc, use /proc/1/ i.e. rely on something under PID 1, not any later PIDs to have reliability/consistency as the later PIDs (processes) are not guranteed to exist.







share|improve this answer


























  • Thanks, I think my question is wrong. I can still lstat files without read access to them. Maybe the access to the folder has to be limited? (I modified the title)

    – Crouching Kitten
    55 mins ago













  • Thanks. With find / -type d -perm 0400 -user root I have found the directory /proc/20/map_files/, if I refer to a made-up file name inside that folder, like /proc/20/map_files/asdasd, then it always fails. Does that folder always exist on Ubuntu?

    – Crouching Kitten
    43 mins ago











  • @CrouchingKitten, the directories in /proc/1/ might be safer, since init always exists. But that's proc, not a regular filesystem, in case it matters.

    – ilkkachu
    37 mins ago











  • Thanks I gave an upvote, but accepted the other answer, because he said it is guaranteed that /proc/1/fdinfo/0 works on modern Ubuntus.

    – Crouching Kitten
    34 mins ago













  • It's the absence of x that prevents the lstat() of files in it, not r.

    – Stéphane Chazelas
    22 mins ago


















4














You can find it yourself.



Using /etc -- the configuration files directory as a starting point:



sudo find /etc -type f -perm 0400 -user root


On my system, this does not return anything.



You can be a less restrictive and allow group root (only user root should be a member of group root), and a look out for a permission of 440:



sudo find /etc -perm 0440 -user root -group root


On my system this returns:



/etc/sudoers.d/README
/etc/sudoers




Edit:



Based on your edit, you're looking for a directory that does not have sufficient permission for the invoking user to prevent directory listing:



sudo find / -perm o-rwx -user root -group root 


here i'm looking for directories (type d) that lacks the read-write-execute perm bits for others (o-rwx) and is owned by root:root.



Technically, just the absense of execute (x) bit would prevent a directory listing (lstat(2)) on directory.



In the output i've found /run/systemd/inaccessible/ on my Systemd init based system.



Regarding files in /proc, /sys, /dev:




  • These filesystems are virtual FS i.e. they reside on memory, not on disk


  • If you plan to rely on /proc, use /proc/1/ i.e. rely on something under PID 1, not any later PIDs to have reliability/consistency as the later PIDs (processes) are not guranteed to exist.







share|improve this answer


























  • Thanks, I think my question is wrong. I can still lstat files without read access to them. Maybe the access to the folder has to be limited? (I modified the title)

    – Crouching Kitten
    55 mins ago













  • Thanks. With find / -type d -perm 0400 -user root I have found the directory /proc/20/map_files/, if I refer to a made-up file name inside that folder, like /proc/20/map_files/asdasd, then it always fails. Does that folder always exist on Ubuntu?

    – Crouching Kitten
    43 mins ago











  • @CrouchingKitten, the directories in /proc/1/ might be safer, since init always exists. But that's proc, not a regular filesystem, in case it matters.

    – ilkkachu
    37 mins ago











  • Thanks I gave an upvote, but accepted the other answer, because he said it is guaranteed that /proc/1/fdinfo/0 works on modern Ubuntus.

    – Crouching Kitten
    34 mins ago













  • It's the absence of x that prevents the lstat() of files in it, not r.

    – Stéphane Chazelas
    22 mins ago
















4












4








4







You can find it yourself.



Using /etc -- the configuration files directory as a starting point:



sudo find /etc -type f -perm 0400 -user root


On my system, this does not return anything.



You can be a less restrictive and allow group root (only user root should be a member of group root), and a look out for a permission of 440:



sudo find /etc -perm 0440 -user root -group root


On my system this returns:



/etc/sudoers.d/README
/etc/sudoers




Edit:



Based on your edit, you're looking for a directory that does not have sufficient permission for the invoking user to prevent directory listing:



sudo find / -perm o-rwx -user root -group root 


here i'm looking for directories (type d) that lacks the read-write-execute perm bits for others (o-rwx) and is owned by root:root.



Technically, just the absense of execute (x) bit would prevent a directory listing (lstat(2)) on directory.



In the output i've found /run/systemd/inaccessible/ on my Systemd init based system.



Regarding files in /proc, /sys, /dev:




  • These filesystems are virtual FS i.e. they reside on memory, not on disk


  • If you plan to rely on /proc, use /proc/1/ i.e. rely on something under PID 1, not any later PIDs to have reliability/consistency as the later PIDs (processes) are not guranteed to exist.







share|improve this answer















You can find it yourself.



Using /etc -- the configuration files directory as a starting point:



sudo find /etc -type f -perm 0400 -user root


On my system, this does not return anything.



You can be a less restrictive and allow group root (only user root should be a member of group root), and a look out for a permission of 440:



sudo find /etc -perm 0440 -user root -group root


On my system this returns:



/etc/sudoers.d/README
/etc/sudoers




Edit:



Based on your edit, you're looking for a directory that does not have sufficient permission for the invoking user to prevent directory listing:



sudo find / -perm o-rwx -user root -group root 


here i'm looking for directories (type d) that lacks the read-write-execute perm bits for others (o-rwx) and is owned by root:root.



Technically, just the absense of execute (x) bit would prevent a directory listing (lstat(2)) on directory.



In the output i've found /run/systemd/inaccessible/ on my Systemd init based system.



Regarding files in /proc, /sys, /dev:




  • These filesystems are virtual FS i.e. they reside on memory, not on disk


  • If you plan to rely on /proc, use /proc/1/ i.e. rely on something under PID 1, not any later PIDs to have reliability/consistency as the later PIDs (processes) are not guranteed to exist.








share|improve this answer














share|improve this answer



share|improve this answer








edited 19 mins ago

























answered 59 mins ago









heemaylheemayl

35.6k376105




35.6k376105













  • Thanks, I think my question is wrong. I can still lstat files without read access to them. Maybe the access to the folder has to be limited? (I modified the title)

    – Crouching Kitten
    55 mins ago













  • Thanks. With find / -type d -perm 0400 -user root I have found the directory /proc/20/map_files/, if I refer to a made-up file name inside that folder, like /proc/20/map_files/asdasd, then it always fails. Does that folder always exist on Ubuntu?

    – Crouching Kitten
    43 mins ago











  • @CrouchingKitten, the directories in /proc/1/ might be safer, since init always exists. But that's proc, not a regular filesystem, in case it matters.

    – ilkkachu
    37 mins ago











  • Thanks I gave an upvote, but accepted the other answer, because he said it is guaranteed that /proc/1/fdinfo/0 works on modern Ubuntus.

    – Crouching Kitten
    34 mins ago













  • It's the absence of x that prevents the lstat() of files in it, not r.

    – Stéphane Chazelas
    22 mins ago





















  • Thanks, I think my question is wrong. I can still lstat files without read access to them. Maybe the access to the folder has to be limited? (I modified the title)

    – Crouching Kitten
    55 mins ago













  • Thanks. With find / -type d -perm 0400 -user root I have found the directory /proc/20/map_files/, if I refer to a made-up file name inside that folder, like /proc/20/map_files/asdasd, then it always fails. Does that folder always exist on Ubuntu?

    – Crouching Kitten
    43 mins ago











  • @CrouchingKitten, the directories in /proc/1/ might be safer, since init always exists. But that's proc, not a regular filesystem, in case it matters.

    – ilkkachu
    37 mins ago











  • Thanks I gave an upvote, but accepted the other answer, because he said it is guaranteed that /proc/1/fdinfo/0 works on modern Ubuntus.

    – Crouching Kitten
    34 mins ago













  • It's the absence of x that prevents the lstat() of files in it, not r.

    – Stéphane Chazelas
    22 mins ago



















Thanks, I think my question is wrong. I can still lstat files without read access to them. Maybe the access to the folder has to be limited? (I modified the title)

– Crouching Kitten
55 mins ago







Thanks, I think my question is wrong. I can still lstat files without read access to them. Maybe the access to the folder has to be limited? (I modified the title)

– Crouching Kitten
55 mins ago















Thanks. With find / -type d -perm 0400 -user root I have found the directory /proc/20/map_files/, if I refer to a made-up file name inside that folder, like /proc/20/map_files/asdasd, then it always fails. Does that folder always exist on Ubuntu?

– Crouching Kitten
43 mins ago





Thanks. With find / -type d -perm 0400 -user root I have found the directory /proc/20/map_files/, if I refer to a made-up file name inside that folder, like /proc/20/map_files/asdasd, then it always fails. Does that folder always exist on Ubuntu?

– Crouching Kitten
43 mins ago













@CrouchingKitten, the directories in /proc/1/ might be safer, since init always exists. But that's proc, not a regular filesystem, in case it matters.

– ilkkachu
37 mins ago





@CrouchingKitten, the directories in /proc/1/ might be safer, since init always exists. But that's proc, not a regular filesystem, in case it matters.

– ilkkachu
37 mins ago













Thanks I gave an upvote, but accepted the other answer, because he said it is guaranteed that /proc/1/fdinfo/0 works on modern Ubuntus.

– Crouching Kitten
34 mins ago







Thanks I gave an upvote, but accepted the other answer, because he said it is guaranteed that /proc/1/fdinfo/0 works on modern Ubuntus.

– Crouching Kitten
34 mins ago















It's the absence of x that prevents the lstat() of files in it, not r.

– Stéphane Chazelas
22 mins ago







It's the absence of x that prevents the lstat() of files in it, not r.

– Stéphane Chazelas
22 mins ago













3














Looking at the lstat(2) man page you can get some inspiration on cases that might make it fail with errors other than ENOENT (file does not exist.)



The most obvious one is:




EACCES
Search permission is denied for one of the directories in the path prefix of path.




So you need a directory you can't read from.



Yes, you can look for one that's already in your system (perhaps /var/lib/private if it exists?) But you might as well create one yourself, with the equivalent of:



$ mkdir myprivatedir
$ touch myprivatedir/myunreachablefile
$ chmod 0 myprivatedir
$ ls -l myprivatedir/myunreachablefile


The lstat(2) operation will fail with EACCES here. (Removing all permissions from the directory ensures that. Maybe you don't even need that much and chmod -x removing execute permissions would be enough, since execute permissions on a directory are needed to access files under it.)



There's another creative way to make lstat(2) fail, looking at its man page:




ENOTDIR
A component of the path prefix of path is not a directory.




So, trying to access a file such as /etc/passwd/nonexistent should trigger this error, which again is different from ENOENT ("No such file or directory") and might suit your needs.



Another one is:




ENAMETOOLONG
path is too long.




But you might need a really long name for this one (I believe 4,096 bytes is the typical limit, but your system/filesystem might have a longer one.)



Finally, it's hard to tell whether any of these will be actually useful for you. You say you want something that doesn't trigger the "file doesn't exist" scenario. While typically that means an ENOENT error, in practice many higher-level checks will simply interpret any errors from lstat(2) as "does not exist". For example test -e or the equivalent [ -e ...] from the shell might simply just interpret all of the above as "does not exist", especially since it doesn't have a good way to return a different error message and not returning an error would imply the file exists, which is most certainly not the case.






share|improve this answer
























  • It's not directories you can't read from, but directories you can't search (for which you don't have the search (x) permission, or to one of its parents).

    – Stéphane Chazelas
    18 mins ago
















3














Looking at the lstat(2) man page you can get some inspiration on cases that might make it fail with errors other than ENOENT (file does not exist.)



The most obvious one is:




EACCES
Search permission is denied for one of the directories in the path prefix of path.




So you need a directory you can't read from.



Yes, you can look for one that's already in your system (perhaps /var/lib/private if it exists?) But you might as well create one yourself, with the equivalent of:



$ mkdir myprivatedir
$ touch myprivatedir/myunreachablefile
$ chmod 0 myprivatedir
$ ls -l myprivatedir/myunreachablefile


The lstat(2) operation will fail with EACCES here. (Removing all permissions from the directory ensures that. Maybe you don't even need that much and chmod -x removing execute permissions would be enough, since execute permissions on a directory are needed to access files under it.)



There's another creative way to make lstat(2) fail, looking at its man page:




ENOTDIR
A component of the path prefix of path is not a directory.




So, trying to access a file such as /etc/passwd/nonexistent should trigger this error, which again is different from ENOENT ("No such file or directory") and might suit your needs.



Another one is:




ENAMETOOLONG
path is too long.




But you might need a really long name for this one (I believe 4,096 bytes is the typical limit, but your system/filesystem might have a longer one.)



Finally, it's hard to tell whether any of these will be actually useful for you. You say you want something that doesn't trigger the "file doesn't exist" scenario. While typically that means an ENOENT error, in practice many higher-level checks will simply interpret any errors from lstat(2) as "does not exist". For example test -e or the equivalent [ -e ...] from the shell might simply just interpret all of the above as "does not exist", especially since it doesn't have a good way to return a different error message and not returning an error would imply the file exists, which is most certainly not the case.






share|improve this answer
























  • It's not directories you can't read from, but directories you can't search (for which you don't have the search (x) permission, or to one of its parents).

    – Stéphane Chazelas
    18 mins ago














3












3








3







Looking at the lstat(2) man page you can get some inspiration on cases that might make it fail with errors other than ENOENT (file does not exist.)



The most obvious one is:




EACCES
Search permission is denied for one of the directories in the path prefix of path.




So you need a directory you can't read from.



Yes, you can look for one that's already in your system (perhaps /var/lib/private if it exists?) But you might as well create one yourself, with the equivalent of:



$ mkdir myprivatedir
$ touch myprivatedir/myunreachablefile
$ chmod 0 myprivatedir
$ ls -l myprivatedir/myunreachablefile


The lstat(2) operation will fail with EACCES here. (Removing all permissions from the directory ensures that. Maybe you don't even need that much and chmod -x removing execute permissions would be enough, since execute permissions on a directory are needed to access files under it.)



There's another creative way to make lstat(2) fail, looking at its man page:




ENOTDIR
A component of the path prefix of path is not a directory.




So, trying to access a file such as /etc/passwd/nonexistent should trigger this error, which again is different from ENOENT ("No such file or directory") and might suit your needs.



Another one is:




ENAMETOOLONG
path is too long.




But you might need a really long name for this one (I believe 4,096 bytes is the typical limit, but your system/filesystem might have a longer one.)



Finally, it's hard to tell whether any of these will be actually useful for you. You say you want something that doesn't trigger the "file doesn't exist" scenario. While typically that means an ENOENT error, in practice many higher-level checks will simply interpret any errors from lstat(2) as "does not exist". For example test -e or the equivalent [ -e ...] from the shell might simply just interpret all of the above as "does not exist", especially since it doesn't have a good way to return a different error message and not returning an error would imply the file exists, which is most certainly not the case.






share|improve this answer













Looking at the lstat(2) man page you can get some inspiration on cases that might make it fail with errors other than ENOENT (file does not exist.)



The most obvious one is:




EACCES
Search permission is denied for one of the directories in the path prefix of path.




So you need a directory you can't read from.



Yes, you can look for one that's already in your system (perhaps /var/lib/private if it exists?) But you might as well create one yourself, with the equivalent of:



$ mkdir myprivatedir
$ touch myprivatedir/myunreachablefile
$ chmod 0 myprivatedir
$ ls -l myprivatedir/myunreachablefile


The lstat(2) operation will fail with EACCES here. (Removing all permissions from the directory ensures that. Maybe you don't even need that much and chmod -x removing execute permissions would be enough, since execute permissions on a directory are needed to access files under it.)



There's another creative way to make lstat(2) fail, looking at its man page:




ENOTDIR
A component of the path prefix of path is not a directory.




So, trying to access a file such as /etc/passwd/nonexistent should trigger this error, which again is different from ENOENT ("No such file or directory") and might suit your needs.



Another one is:




ENAMETOOLONG
path is too long.




But you might need a really long name for this one (I believe 4,096 bytes is the typical limit, but your system/filesystem might have a longer one.)



Finally, it's hard to tell whether any of these will be actually useful for you. You say you want something that doesn't trigger the "file doesn't exist" scenario. While typically that means an ENOENT error, in practice many higher-level checks will simply interpret any errors from lstat(2) as "does not exist". For example test -e or the equivalent [ -e ...] from the shell might simply just interpret all of the above as "does not exist", especially since it doesn't have a good way to return a different error message and not returning an error would imply the file exists, which is most certainly not the case.







share|improve this answer












share|improve this answer



share|improve this answer










answered 32 mins ago









filbrandenfilbranden

9,24621342




9,24621342













  • It's not directories you can't read from, but directories you can't search (for which you don't have the search (x) permission, or to one of its parents).

    – Stéphane Chazelas
    18 mins ago



















  • It's not directories you can't read from, but directories you can't search (for which you don't have the search (x) permission, or to one of its parents).

    – Stéphane Chazelas
    18 mins ago

















It's not directories you can't read from, but directories you can't search (for which you don't have the search (x) permission, or to one of its parents).

– Stéphane Chazelas
18 mins ago





It's not directories you can't read from, but directories you can't search (for which you don't have the search (x) permission, or to one of its parents).

– Stéphane Chazelas
18 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502857%2fis-there-a-file-that-always-exists-and-a-normal-user-cant-lstat-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...