How does bash make a file executable?

0

I know that when I run the command

chmod +x filename

bash will make my file an executable. What exactly happens to the architecture of the file to change it to an executable? i.e. how does the kernel know that this file is executable?

Just trying to exercise my nerd muscles.

StatGenGeek

Posted 2017-03-20T05:11:49.627

Reputation: 111

Answers

6

The "architecture" of the file doesn't change – it's up to you to make sure it has a correct format. That is, the file already needs to be a valid ELF binary, or a script with a valid #! header, or something else that your particular kernel recognizes (like a.out or MZ/PE).

What chmod does is just set a bit in the file's permissions telling the kernel that the user is allowed to execute this file. It can act as a small security barrier, as well as prevent certain kinds of accidents. (Note that +x here is shorthand for ugo+x; it's a permission bit just like read/write.)

If you set +x on a file that the kernel doesn't recognize, it'll just return an error code from the exec syscall. (Note that some shells have special handling for this – if the kernel reported "unrecognized format", the shell will try to interpret the file as a script internally, as that's how shellscripts worked before #! was invented.)

user1686

Posted 2017-03-20T05:11:49.627

Reputation: 283 655

"If you set +x on a file that the kernel doesn't recognize, it'll just return an error code from the exec syscall" -- it's not obvious when this error is returned. It's when you try to execute, not when you chmod; right? – Kamil Maciorowski – 2017-03-21T06:39:23.087

Well, it cannot possibly refer to execution of the 'chmod' command, as that happens way before anything mode-related (the actual kernel operation is the chmod syscall anyway). So that leaves (attempted) execution of the file in question. – user1686 – 2017-03-21T07:40:53.810

That makes sense thank you. Sorry for the poorly worded question. I couldn't think of how else to describe what I was asking. 'set a bit in the file's permissions' is what I was wanting to know. – StatGenGeek – 2017-03-22T03:10:17.953

0

The file's contents don't change at all. Filesystems that are designed for typical Unix will support storing information about every file. This is often called the file's "meta data".

This is why you won't get similar results with some filesystems. Namely, FAT doesn't have built-in space designed for storing data related to Unix permissions, and Unix typically gets around that by using a special parameter when mounting such a filesystem volume. So, typically all files on a FAT drive have the same permissions, as specified by mounting. (I would expect similar behavior for other filesystems, like ISO9660, although that is probably usually unwritable.) On volumes mounted in this way, using chmod +x won't actually end up changing anything.

In short, the way the kernel knows this is by checking the filesystem driver. For typical filesystems designed for unix, that driver supports reading the locations where Unix-style permissions are stored.

chmod simply changes those spots on the drive.

TOOGAM

Posted 2017-03-20T05:11:49.627

Reputation: 12 651

I'm really not disagreeing with grawity 's answer at all. I just think grawity's answer focuses more on how the kernel interprets the file data after the operating system determines that the file should be treated as an executable, while my answer focuses more on what chmod does. So I answer the "How does bash" (title question)... and "What... executable", while I let grawity's answer remain for addressing "How does the kernel"...

– TOOGAM – 2017-03-21T06:12:03.963