Skip to contentSkip to conversation

Does struct qstr->name should be terminated ?

DISCUSSION Jerome de Vivie 4 participants 5 messages Mar 23, 2004 – Mar 25, 2004 VFS

lore
Share & tools
lore
#0 lore raw

I've work with "struct qstr" for a while without bothering with the last caracter: qstr->name[qstr->len]. I get oops from my module but after investigation, i found lines in fs/namei.c that are using this caracter:

In do_rename:

        /* unless the source is a directory trailing slashes give 
-ENOTDIR */
        if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
                error = -ENOTDIR;
                if (oldnd.last.name[oldnd.last.len])
                        goto exit4;
                if (newnd.last.name[newnd.last.len])
                        goto exit4;
        }

So, do i need to initialize this caracter when allocating new qstr ? Is this caracter is used in other place in the VFS ?

regards,

j.

#1 lore raw
Quoted 16 lines · Jerome de Vivie

I've work with "struct qstr" for a while without bothering with the last
caracter: qstr->name[qstr->len]. I get oops from my module but after investigation, i found lines in fs/namei.c that are using this caracter:

In do_rename:

        /* unless the source is a directory trailing slashes give 
-ENOTDIR */
        if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
                error = -ENOTDIR;
                if (oldnd.last.name[oldnd.last.len])
                        goto exit4;
                if (newnd.last.name[newnd.last.len])
                        goto exit4;
        }
Quoted 2 lines

So, do i need to initialize this caracter when allocating new qstr ? Is this caracter is used in other place in the VFS ?

I'd think that it's treated as a nomral string in quite a few places, so it needs to be NUL terminated. (But not searched the sources.)

-------------------------------------------------------------------------------
Jan 'Bulb' Hudec <[email protected]>

#2 lore raw
Quoted 2 lines · Jan Hudec

I'd think that it's treated as a nomral string in quite a few places, so it needs to be NUL terminated. (But not searched the sources.)

Why not? It isn't hard to find.

fs/dcache.c:703: str[name->len] = 0;

Signature · 6 lines
"Next the statesmen will invent cheap lies, putting the blame upon 
the nation that is attacked, and every man will be glad of those
conscience-soothing falsities, and will diligently study them, and refuse
to examine any refutations of them; and thus he will by and by convince 
himself that the war is just, and will thank God for the better sleep 
he enjoys after this process of grotesque self-deception." -- Mark Twain
#3 lore raw
Quoted 1 line
Quoted 1 line

I'd think that it's treated as a nomral string in quite a few places,

so

Quoted 5 lines
Quoted 1 line

it needs to be NUL terminated. (But not searched the sources.)

Why not? It isn't hard to find.

fs/dcache.c:703: str[name->len] = 0;

That's technically an area where it's treated like a normal string, but not really relevant to the question (which is "what if I don't nul-terminate the thing"? Do you have an example handy of a strcmp() or such that would fail if the NUL terminator were not there?

#4 lore raw
Quoted 4 lines · Bryan Henderson
Quoted 1 line
Quoted 1 line

I'd think that it's treated as a nomral string in quite a few places,

so

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

it needs to be NUL terminated. (But not searched the sources.)

Why not? It isn't hard to find.

fs/dcache.c:703: str[name->len] = 0;

That's technically an area where it's treated like a normal string, but

I thougth that qstr->name was not treated as a normal string and has been design to speedup d_compare test with strncmp instead of strcmp. That's why i never bother with the null termination caracter.

Quoted 1 line

not really relevant to the question (which is "what if I don't nul-terminate the thing"? Do you have an example handy of a strcmp() or such that would fail if the NUL terminator were not there?

In yourfs->d_lookup, you can reallocate qstr and pad d_name with random caracters. After that, unexpected behavior could happen after a mv(1):
the VFS could increment qstr->len. The result is one more caracter for some entries after a ls(1).