Skip to content

Linux store reads GError.message from the wrong offset on 64-bit, so every libsecret failure yields a garbage message (or a crash) #163

Description

@matt-edmondson

What's wrong

LinuxSecretServiceCredentialStore.ThrowIfError (CredentialCache/Storage/LinuxSecretServiceCredentialStore.cs:142) reads the error message like this:

IntPtr messagePtr = Marshal.ReadIntPtr(error, IntPtr.Size * 2);

The glib layout is GError { GQuark domain /*uint32*/; gint code /*int32*/; gchar *message; }. So on 64-bit the message pointer is at offset 8, and the struct is 16 bytes long. IntPtr.Size * 2 is 16, which reads the word after the end of the struct. On 32-bit, IntPtr.Size * 2 happens to equal 8, which is why the bug is easy to miss.

Failure scenario (verified)

  • A C program built against the system glib headers prints sizeof(GError)=16 offsetof(message)=8.
  • A .NET app created a real GError with g_error_new_literal:
    • reading at offset 8 returned the correct message;
    • reading at offset 16 returned 0x20, which is the next malloc chunk's header;
    • a verbatim copy of the ThrowIfError extraction produced failed: ��.

The most common trigger is the one the class docs tell consumers to handle: a headless machine or container with no Secret Service running. The real libsecret error text is lost, the CredentialStoreException message is heap garbage, and if the stray value points at unmapped memory, PtrToStringUTF8 faults the process.

Suggested fix

Declare the struct and marshal it:

[StructLayout(LayoutKind.Sequential)]
private struct GError { public uint Domain; public int Code; public IntPtr Message; }
...
GError e = Marshal.PtrToStructure<GError>(error);
message = Marshal.PtrToStringUTF8(e.Message);

The minimal alternative is to read at sizeof(uint) + sizeof(int), which is 8 on both architectures.

Acceptance: on Linux x64 with no D-Bus session, the thrown exception carries libsecret's real message. For example, "Cannot autolaunch D-Bus without X11 $DISPLAY", or whatever the host reports.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

readyFully specified; implement as written

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions