git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] attr: fix msan issue in read_attr_from_index

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 17, 2024, 20:30 UTC
Message-ID
<xmqqcyof5n2t.fsf@gitster.g>
In-Reply-To
<pull.1747.git.1718654424683.gitgitgadget@gmail.com>
"Kyle Lippincott via GitGitGadget" <gitgitgadget@gmail.com> writes:
> The issue exists because `size` is an output parameter from
> `read_blob_data_from_index`, but it's only modified if
> `read_blob_data_from_index` returns non-NULL.
Correct.
Show 5 quoted lines
> The read of `size` when
> calling `read_attr_from_buf` unconditionally may read from an
> uninitialized value. `read_attr_from_buf` checks that `buf` is non-NULL
> before reading from `size`, but by then it's already too late: the
> uninitialized read will have happened already.

Yes, but it is dubious that reading an uninitialized value that we know will not be used is a problem, so I am inclined to say that MSAN is giving a false positive here.

> Furthermore, there's no
> guarantee that the compiler won't reorder things so that it checks
> `size` before checking `!buf`.

This I do not understand. Are you talking about buf vs length here in the callee?

        static struct attr_stack *read_attr_from_buf(char *buf, size_t length,
                                                     const char *path, unsigned flags)
        {
                struct attr_stack *res;
                char *sp;
                int lineno = 0;
                if (!buf)
                        return NULL;
                if (length >= ATTR_MAX_FILE_SIZE) {
                        warning(_("ignoring overly large gitattributes blob '%s'"), path);
                        free(buf);
                        return NULL;
                }

At the machine level, a prefetch may happen from both buf and length, but the program ought to behave the same way as the code is executed serially as written. If the compiler allows the outside world to observe that resulting code checks length even when buf is NULL, such a compiler is broken. So I do not think that is what you are referring to, but then I do not know what problem you are describing.

Having said all that ...
> Make the call to `read_attr_from_buf` conditional on `buf` being
> non-NULL, ensuring that `size` is not read if it's never set.

... this makes the logic at the caller crystal clear, so even if there are suboptimal checker that bothers us with false positives, the change itself justifies itself, I would say.

Show 7 quoted lines
>  	} else {
>  		buf = read_blob_data_from_index(istate, path, &size);
> -		stack = read_attr_from_buf(buf, size, path, flags);
> +		if (buf)
> +			stack = read_attr_from_buf(buf, size, path, flags);
>  	}
>  	return stack;
Thanks.
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 6 in “attr: fix msan issue in read_attr_from_index”
  1. attr: fix msan issue in read_attr_from_indexKyle Lippincott via GitGitGadget, Jun 17, 2024
  2. Junio C HamanoJun 17, 2024
  3. Junio C HamanoJun 17, 2024
  4. Junio C HamanoJun 17, 2024
  5. Kyle LippincottJun 17, 2024
  6. Jeff KingJun 18, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.