Bug 249162 - Consider passing argc/argv/env to the shared object init function (DT_INIT) for compatibility with glibc
Summary: Consider passing argc/argv/env to the shared object init function (DT_INIT) f...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: Unspecified
Hardware: Any Any
: --- Affects Only Me
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-09-07 09:53 UTC by Alex S
Modified: 2020-09-14 12:23 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex S 2020-09-07 09:53:51 UTC
% cat so_init.c 
#include <stdio.h>

void so_init(int argc, char** argv, char** env) {
  for (int i = 0; i < argc; i++) {
    printf("arg[%d]: %s\n", i, argv[i]);
  }
}
% cc -std=c99 -fPIC -shared -Wl,-init=so_init so_init.c -o test.so
% env LD_PRELOAD=$PWD/test.so true 1 2 3
Bus error
% /compat/linux/bin/cc -std=c99 -fPIC -shared -Wl,-init=so_init so_init.c -o test.so
% env LD_PRELOAD=$PWD/test.so /compat/linux/bin/true 1 2 3 
arg[0]: /compat/linux/bin/true
arg[1]: 1
arg[2]: 2
arg[3]: 3

Some notes there:
1. relevant glibc code: https://sourceware.org/git/?p=glibc.git;a=blob;f=elf/dl-init.c;h=3e72fa3013a6aaeda05fe61a0ae7af5d46640826;hb=HEAD#l58 ;
2. rtld already does this for functions referenced in DT_INIT_ARRAY, so it's enough to replace a call_initfini_pointer call with call_init_pointer.
Comment 1 Bob Bishop 2020-09-07 10:48:34 UTC
Doesn't this have unpleasant security implications?
Comment 2 Alex S 2020-09-07 10:55:02 UTC
(In reply to Bob Bishop from comment #1)

I'm not aware of any security implications.
Comment 3 Konstantin Belousov freebsd_committer 2020-09-07 11:39:24 UTC
https://reviews.freebsd.org/D26351
Comment 4 commit-hook freebsd_committer 2020-09-07 21:32:40 UTC
A commit references this bug:

Author: kib
Date: Mon Sep  7 21:32:28 UTC 2020
New revision: 365432
URL: https://svnweb.freebsd.org/changeset/base/365432

Log:
  rtld: pass argc/argv/env to dso inits.

  This is consistent with how array inits are called, and also makes us
  more compatible with glibc environment.

  Requested by:	Alex S <iwtcex@gmail.com>
  PR:	249162
  Reviewed by:	dim, emaste
  Sponsored by:	The FreeBSD Foundation
  MFC after:	1 week
  Differential revision:	https://reviews.freebsd.org/D26351

Changes:
  head/libexec/rtld-elf/rtld.c
Comment 5 Alex S 2020-09-08 08:48:09 UTC
(In reply to commit-hook from comment #4)

Thank you, that should be quite useful.
Comment 6 commit-hook freebsd_committer 2020-09-14 10:49:53 UTC
A commit references this bug:

Author: kib
Date: Mon Sep 14 10:49:39 UTC 2020
New revision: 365714
URL: https://svnweb.freebsd.org/changeset/base/365714

Log:
  MFC r365432:
  rtld: pass argc/argv/env to dso inits.

  PR:	249162

Changes:
_U  stable/12/
  stable/12/libexec/rtld-elf/rtld.c
Comment 7 commit-hook freebsd_committer 2020-09-14 11:02:58 UTC
A commit references this bug:

Author: kib
Date: Mon Sep 14 11:02:41 UTC 2020
New revision: 365716
URL: https://svnweb.freebsd.org/changeset/base/365716

Log:
  MFC r365432:
  rtld: pass argc/argv/env to dso inits.

  PR:	249162

Changes:
_U  stable/11/
  stable/11/libexec/rtld-elf/rtld.c