public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug dynamic-link/29001] New: LD_AUDIT doesn't work with statically linked but dynamically loaded applications using TLS
@ 2022-03-25 14:33 maksym.polovyi at dynatrace dot com
  0 siblings, 0 replies; only message in thread
From: maksym.polovyi at dynatrace dot com @ 2022-03-25 14:33 UTC (permalink / raw)
  To: glibc-bugs

https://sourceware.org/bugzilla/show_bug.cgi?id=29001

            Bug ID: 29001
           Summary: LD_AUDIT doesn't work with statically linked but
                    dynamically loaded applications using TLS
           Product: glibc
           Version: 2.34
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: dynamic-link
          Assignee: unassigned at sourceware dot org
          Reporter: maksym.polovyi at dynatrace dot com
  Target Milestone: ---

Hi, we are observing an issue when using LD_AUDIT and statically linked, but
dynamically loaded application with TLS.

The issue is
```
Inconsistency detected by ld.so: ../elf/dl-tls.c: 571: _dl_allocate_tls_init:
Assertion `listp->slotinfo[cnt].gen <= GL(dl_tls_generation)' failed!
```

### Steps to reproduce:
 * Compile `audit.c` like `gcc -shared -fPIE -o libaudit.so audit.c`
 * Compile reference go binary `go-app.go` like `CGO_ENABLED=0 go build
-buildmode=pie go-app.go`
 * Run go app with LD_AUDIT set:
```
LD_AUDIT=$(pwd)/libaudit.so ./go-app
```

Added Makefile for convenience:
```
make audit
make app
make run
```

### After some investigation, I can say that:
 * TLS is unconditionally init-ed for audit modules
 * the TLS-enabled app is added to the TLS-required list
 * the audit library is not counted in the `_dl_count_modids` at
https://elixir.bootlin.com/glibc/glibc-2.34/source/elf/rtld.c#L2413
 * so when the TLS is being inited for the app, the dl_tls_generation is still
0, when it should be 1

I would suspect that counting audit libraries in the `_dl_count_modids` could
be a possible fix.
Also I failed to understand why the TLS is inited unconditionally for the audit
block.
Not arguing that it's not needed, but would be curious to know why it's there,
even if the audit library is not using any TLS variables.

Things attempted to resolve the issue:
* Use TLS in the audit library - didn't work with any tls model
* Avoid using TLS in the application - not an option so didn't check




Didn't figure out how to add multiple attachments:
``` audit.c
unsigned int la_version(unsigned int version) {  
    return version;
}

char * la_objsearch(const char *name, int *cookie, unsigned int flag) {
        return (char*)name;
}
```

``` go-app.go
package main
import "fmt"
func main() {
    fmt.Println("hello world")
}
```

``` Makefile
audit:
        gcc -shared -fPIE -o libaudit.so audit.c

app:
        CGO_ENABLED=0 go build -buildmode=pie go-app.go

current_path := $(shell pwd)
run:
        LD_AUDIT=$(current_path)/libaudit.so ./go-app
```

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-25 14:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-25 14:33 [Bug dynamic-link/29001] New: LD_AUDIT doesn't work with statically linked but dynamically loaded applications using TLS maksym.polovyi at dynatrace dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).