1. 14 3月, 2019 1 次提交
  2. 13 3月, 2019 5 次提交
    • R
      handle labels with 8-bit byte values in dn_skipname · 2a0ff45b
      Ryan Fairfax 提交于
      The original logic considered each byte until it either found a 0
      value or a value >= 192. This means if a string segment contained any
      byte >= 192 it was interepretted as a compressed segment marker even
      if it wasn't in a position where it should be interpretted as such.
      
      The fix is to adjust dn_skipname to increment by each segments size
      rather than look at each character. This avoids misinterpretting
      string segment characters by not considering those bytes.
      2a0ff45b
    • J
      fix POSIX_FADV_DONTNEED/_NOREUSE on s390x · 4b125dd4
      Jonathan Neuschäfer 提交于
      On s390x, POSIX_FADV_DONTNEED and POSIX_FADV_NOREUSE have different
      values than on all other architectures that Linux supports.
      
      Handle this difference by wrapping their definitions in
      include/fcntl.h in #ifdef, so that arch/s390x/bits/fcntl.h can
      override them.
      4b125dd4
    • R
      expose TSVTX unconditionally in tar.h · 81221e13
      Rich Felker 提交于
      as noted in Austin Group issue #1236, the XSI shading for TSVTX is
      misplaced in the html version of the standard; it was only supposed to
      be on the description text. the intent was that the definition always
      be visible, which is reflected in the pdf version of the standard.
      
      this reverts commits d93c0740 and
      729fef0a.
      81221e13
    • A
      setvbuf: return failure if mode is invalid · 00d3d577
      A. Wilcox 提交于
      POSIX requires setvbuf to return non-zero if `mode` is not one of _IONBF,
      _IOLBF, or _IOFBF.
      00d3d577
    • R
      make FILE a complete type for pre-C11 standard profiles · f368d9fd
      Rich Felker 提交于
      C11 removed the requirement that FILE be a complete type, which was
      deemed erroneous, as part of the changes introduced by N1439 regarding
      completeness of types (see footnote 6 for specific mention of FILE).
      however the current version of POSIX is still based on C99 and
      incorporates the old requirement that FILE be a complete type.
      
      expose an arbitrary, useless complete type definition because the
      actual object used to represent FILE streams cannot be public/ABI.
      
      thanks to commit 13d1afa4, we now have
      a framework for suppressing the public complete-type definition of FILE
      when stdio.h is included internally, so that a different internal
      definition can be provided. this is perfectly well-defined, since the
      same struct tag can refer to different types in different translation
      units. it would be a problem if the implementation were accessing the
      application's FILE objects or vice versa, but either would be
      undefined behavior.
      f368d9fd
  3. 11 3月, 2019 1 次提交
    • R
      fix invalid-/double-/use-after-free in new dlopen ctor execution · 50cd0238
      Rich Felker 提交于
      this affected the error path where dlopen successfully found and
      loaded the requested dso and all its dependencies, but failed to
      resolve one or more relocations, causing the operation to fail after
      storage for the ctor queue was allocated.
      
      commit 188759bb wrongly put the free
      for the ctor_queue array in the error path inside a loop over each
      loaded dso that needed to be backed-out, rather than just doing it
      once. in addition, the exit path also observed the ctor_queue pointer
      still being nonzero, and would attempt to call ctors on the backed-out
      dsos unless the double-free crashed the process first.
      50cd0238
  4. 06 3月, 2019 1 次提交
    • R
      don't reject unknown/future flags in sigaltstack, allow SS_AUTODISARM · 4918b7fb
      Rich Felker 提交于
      historically, and likely accidentally, sigaltstack was specified to
      fail with EINVAL if any flag bit other than SS_DISABLE was set. the
      resolution of Austin Group issue 1187 fixes this so that the
      requirement is only to fail for SS_ONSTACK (which cannot be set) or
      "invalid" flags.
      
      Linux fails on the kernel side for invalid flags, but historically
      accepts SS_ONSTACK as a no-op, so it needs to be rejected in userspace
      still.
      
      with this change, the Linux-specific SS_AUTODISARM, provided since
      commit 9680e1d0 but unusable due to
      rejection at runtime, is now usable.
      4918b7fb
  5. 04 3月, 2019 6 次提交
    • R
      avoid malloc of ctor queue for programs with no external deps · 43e7efb4
      Rich Felker 提交于
      together with the previous two commits, this completes restoration of
      the property that dynamic-linked apps with no external deps and no tls
      have no failure paths before entry.
      43e7efb4
    • R
      avoid malloc of deps arrays for ldso and vdso · f034f145
      Rich Felker 提交于
      neither has or can have any dependencies, but since commit
      40355569, gratuitous zero-length deps
      arrays were being allocated for them. use a dummy array instead.
      f034f145
    • R
      avoid malloc of deps array for programs with no external deps · e612d094
      Rich Felker 提交于
      traditionally, we've provided a guarantee that dynamic-linked
      applications with no external dependencies (nothing but libc) and no
      thread-local storage have no failure paths before the entry point.
      normally, thanks to reclaim_gaps, such a malloc will not require a
      syscall anyway, but if segment alignment is unlucky, it might. use a
      builtin array for this common special case.
      e612d094
    • R
      fix malloc misuse for startup ctor queue, breakage on fdpic archs · 2f1f51ae
      Rich Felker 提交于
      in the case where malloc is being replaced, it's not valid to call
      malloc between final relocations and main app's crt1 entry point; on
      fdpic archs the main app's entry point will not yet have performed the
      self-fixups necessary to call its code.
      
      to fix, reorder queue_ctors before final relocations. an alternative
      solution would be doing the allocation from __libc_start_init, after
      the entry point but before any ctors run. this is less desirable,
      since it would leave a call to malloc that might be provided by the
      application happening at startup when doing so can be easily avoided.
      2f1f51ae
    • R
      synchronize shared library dtor exec against concurrent loads/ctors · 8e43b561
      Rich Felker 提交于
      previously, going way back, there was simply no synchronization here.
      a call to exit concurrent with ctor execution from dlopen could cause
      a dtor to execute concurrently with its corresponding ctor, or could
      cause dtors for newly-constructed libraries to be skipped.
      
      introduce a shutting_down state that blocks further ctor execution,
      producing the quiescence the dtor execution loop needs to ensure any
      kind of consistency, and that blocks further calls to dlopen so that a
      call into dlopen from a dtor cannot deadlock.
      
      better approaches to some of this may be possible, but the changes
      here at least make things safe.
      8e43b561
    • R
      overhaul shared library ctor execution for dependency order, concurrency · 188759bb
      Rich Felker 提交于
      previously, shared library constructors at program start and dlopen
      time were executed in reverse load order. some libraries, however,
      rely on a depth-first dependency order, which most other dynamic
      linker implementations provide. this is a much more reasonable, less
      arbitrary order, and it turns out to have much better properties with
      regard to how slow-running ctors affect multi-threaded programs, and
      how recursive dlopen behaves.
      
      this commit builds on previous work tracking direct dependencies of
      each dso (commit 40355569), and
      performs a topological sort on the dependency graph at load time while
      the main ldso lock is held and before success is committed, producing
      a queue of constructors needed by the newly-loaded dso (or main
      application). in the case of circular dependencies, the dependency
      chain is simply broken at points where it becomes circular.
      
      when the ctor queue is run, the init_fini_lock is held only for
      iteration purposes; it's released during execution of each ctor, so
      that arbitrarily-long-running application code no longer runs with a
      lock held in the caller. this prevents a dlopen with slow ctors in one
      thread from arbitrarily delaying other threads that call dlopen.
      fully-independent ctors can run concurrently; when multiple threads
      call dlopen with a shared dependency, one will end up executing the
      ctor while the other waits on a condvar for it to finish.
      
      another corner case improved by these changes is recursive dlopen
      (call from a ctor). previously, recursive calls to dlopen could cause
      a ctor for a library to be executed before the ctor for its
      dependency, even when there was no relation between the calling
      library and the library it was loading, simply due to the naive
      reverse-load-order traversal. now, we can guarantee that recursive
      dlopen in non-circular-dependency usage preserves the desired ctor
      execution order properties, and that even in circular usage, at worst
      the libraries whose ctors call dlopen will fail to have completed
      construction when ctors that depend on them run.
      
      init_fini_lock is changed to a normal, non-recursive mutex, since it
      is no longer held while calling back into application code.
      188759bb
  6. 03 3月, 2019 2 次提交
  7. 28 2月, 2019 2 次提交
    • R
      fix and overhaul dlsym depedency order, always record direct deps · 40355569
      Rich Felker 提交于
      dlsym with an explicit handle is specified to use "dependency order",
      a breadth-first search rooted at the argument. this has always been
      implemented by iterating a flattened dependency list built at dlopen
      time. however, the logic for building this list was completely wrong
      except in trivial cases; it simply used the list of libraries loaded
      since a given library, and their direct dependencies, as that
      library's dependencies, which could result in misordering, wrongful
      omission of deep dependencies from the search, and wrongful inclusion
      of unrelated libraries in the search.
      
      further, libraries did not have any recorded list of resolved
      dependencies until they were explicitly dlopened, meaning that
      DT_NEEDED entries had to be resolved again whenever a library
      participated as a dependency of more than one dlopened library.
      
      with this overhaul, the resolved direct dependency list of each
      library is always recorded when it is first loaded, and can be
      extended to a full flattened breadth-first search list if dlopen is
      called on the library. the extension is performed using the direct
      dependency list as a queue and appending copies of the direct
      dependency list of each dependency in the queue, excluding duplicates,
      until the end of the queue is reached. the direct deps remain
      available for future use as the initial subarray of the full deps
      array.
      
      first-load logic in dlopen is updated to match these changes, and
      clarified.
      40355569
    • R
      fix crash/misbehavior from oob read in new dynamic tls installation · 71db5dfa
      Rich Felker 提交于
      code introduced in commit 9d44b646
      wrongly attempted to read past the end of the currently-installed dtv
      to determine if a dso provides new, not-already-installed tls. this
      logic was probably leftover from an earlier draft of the code that
      wrongly installed the new dtv before populating it.
      
      it would work if we instead queried the new, not-yet-installed dtv,
      but instead, replace the incorrect check with a simple range check
      against old_cnt. this also catches modules that have no tls at all
      with a single condition.
      71db5dfa
  8. 25 2月, 2019 1 次提交
    • R
      fix crash in new dynamic tls installation when last dep lacks tls · 6516282d
      Rich Felker 提交于
      code introduced in commit 9d44b646
      wrongly assumed the dso list tail was the right place to find new dtv
      storage. however, this is only true if the last-loaded dependency has
      tls. the correct place to get it is the dso corresponding to the tls
      module list tail. introduce a container_of macro to get it, and use
      it.
      
      ultimately, dynamic tls allocation should be refactored so that this
      is not an issue. there is no reason to be allocating new dtv space at
      each load_library; instead it could happen after all new libraries
      have been loaded but before they are committed. such changes may be
      made later, but this commit fixes the present regression.
      6516282d
  9. 22 2月, 2019 3 次提交
    • R
      add membarrier syscall wrapper, refactor dynamic tls install to use it · ba18c1ec
      Rich Felker 提交于
      the motivation for this change is twofold. first, it gets the fallback
      logic out of the dynamic linker, improving code readability and
      organization. second, it provides application code that wants to use
      the membarrier syscall, which depends on preregistration of intent
      before the process becomes multithreaded unless unbounded latency is
      acceptable, with a symbol that, when linked, ensures that this
      registration happens.
      ba18c1ec
    • R
      make thread list lock a recursive lock · 7865d569
      Rich Felker 提交于
      this is a prerequisite for factoring the membarrier fallback code into
      a function that can be called from a context with the thread list
      already locked or independently.
      7865d569
    • R
      fix loop logic cruft in dynamic tls installation · 609dd57c
      Rich Felker 提交于
      commit 9d44b646 inadvertently
      contained leftover logic from a previous approach to the fallback
      signaling loop. it had no adverse effect, since j was always nonzero
      if the loop body was reachable, but it makes no sense to be there with
      the current approach to avoid signaling self.
      609dd57c
  10. 21 2月, 2019 2 次提交
    • R
      fix spurious undefined behavior in getaddrinfo · ad795d56
      Rich Felker 提交于
      addressing &out[k].sa was arguably undefined, despite &out[k] being
      defined the slot one past the end of an array, since the member access
      .sa is intervening between the [] operator and the & operator.
      ad795d56
    • R
      fix invalid free of partial addrinfo list with multiple services · 224d938c
      Rich Felker 提交于
      the backindex stored by getaddrinfo to allow freeaddrinfo to perform
      partial-free wrongly used the address result index, rather than the
      output slot index, and thus was only valid when they were equal
      (nservs==1).
      
      patch based on report with proposed fix by Markus Wichmann.
      224d938c
  11. 19 2月, 2019 1 次提交
    • R
      install dynamic tls synchronously at dlopen, streamline access · 9d44b646
      Rich Felker 提交于
      previously, dynamic loading of new libraries with thread-local storage
      allocated the storage needed for all existing threads at load-time,
      precluding late failure that can't be handled, but left installation
      in existing threads to take place lazily on first access. this imposed
      an additional memory access and branch on every dynamic tls access,
      and imposed a requirement, which was not actually met, that the
      dynamic tlsdesc asm functions preserve all call-clobbered registers
      before calling C code to to install new dynamic tls on first access.
      the x86[_64] versions of this code wrongly omitted saving and
      restoring of fpu/vector registers, assuming the compiler would not
      generate anything using them in the called C code. the arm and aarch64
      versions saved known existing registers, but failed to be future-proof
      against expansion of the register file.
      
      now that we track live threads in a list, it's possible to install the
      new dynamic tls for each thread at dlopen time. for the most part,
      synchronization is not needed, because if a thread has not
      synchronized with completion of the dlopen, there is no way it can
      meaningfully request access to a slot past the end of the old dtv,
      which remains valid for accessing slots which already existed.
      however, it is necessary to ensure that, if a thread sees its new dtv
      pointer, it sees correct pointers in each of the slots that existed
      prior to the dlopen. my understanding is that, on most real-world
      coherency architectures including all the ones we presently support, a
      built-in consume order guarantees this; however, don't rely on that.
      instead, the SYS_membarrier syscall is used to ensure that all threads
      see the stores to the slots of their new dtv prior to the installation
      of the new dtv. if it is not supported, the same is implemented in
      userspace via signals, using the same mechanism as __synccall.
      
      the __tls_get_addr function, variants, and dynamic tlsdesc asm
      functions are all updated to remove the fallback paths for claiming
      new dynamic tls, and are now all branch-free.
      9d44b646
  12. 18 2月, 2019 1 次提交
    • R
      fix data race between new pthread_key_delete and dtor execution · 80528892
      Rich Felker 提交于
      access to clear the entry in each thread's tsd array for the key being
      deleted was not synchronized with __pthread_tsd_run_dtors. I probably
      made this mistake from a mistaken belief that the thread list lock was
      held during the latter, which of course is not possible since it
      executes application code in a still-live-thread context.
      
      while we're at it, expand the interval during which signals are
      blocked to cover taking the write lock on key_lock, so that a signal
      at an inopportune time doesn't block forward progress of readers.
      80528892
  13. 17 2月, 2019 1 次提交
  14. 16 2月, 2019 6 次提交
    • R
      rewrite pthread_key_delete to use global thread list · ba74a42c
      Rich Felker 提交于
      with the availability of the thread list, there is no need to mark tsd
      key slots dirty and clean them up only when a free slot can't be
      found. instead, directly iterate threads and clear any value
      associated with the key being deleted.
      
      no synchronization is necessary for the clearing, since there is no
      way the slot can be accessed without having synchronized with the
      creation of a new key occupying the same slot, which is already
      sequenced after and synchronized with the deletion of the old key.
      ba74a42c
    • R
      rewrite __synccall in terms of global thread list · e4235d70
      Rich Felker 提交于
      the __synccall mechanism provides stop-the-world synchronous execution
      of a callback in all threads of the process. it is used to implement
      multi-threaded setuid/setgid operations, since Linux lacks them at the
      kernel level, and for some other less-critical purposes.
      
      this change eliminates dependency on /proc/self/task to determine the
      set of live threads, which in addition to being an unwanted dependency
      and a potential point of resource-exhaustion failure, turned out to be
      inaccurate. test cases provided by Alexey Izbyshev showed that it
      could fail to reflect newly created threads. due to how the
      presignaling phase worked, this usually yielded a deadlock if hit, but
      in the worst case it could also result in threads being silently
      missed (allowed to continue running without executing the callback).
      e4235d70
    • R
      track all live threads in an AS-safe, fully-consistent linked list · 8f11e612
      Rich Felker 提交于
      the hard problem here is unlinking threads from a list when they exit
      without creating a window of inconsistency where the kernel task for a
      thread still exists and is still executing instructions in userspace,
      but is not reflected in the list. the magic solution here is getting
      rid of per-thread exit futex addresses (set_tid_address), and instead
      using the exit futex to unlock the global thread list.
      
      since pthread_join can no longer see the thread enter a detach_state
      of EXITED (which depended on the exit futex address pointing to the
      detach_state), it must now observe the unlocking of the thread list
      lock before it can unmap the joined thread and return. it doesn't
      actually have to take the lock. for this, a __tl_sync primitive is
      offered, with a signature that will allow it to be enhanced for quick
      return even under contention on the lock, if needed. for now, the
      exiting thread always performs a futex wake on its detach_state. a
      future change could optimize this out except when there is already a
      joiner waiting.
      
      initial/dynamic variants of detached state no longer need to be
      tracked separately, since the futex address is always set to the
      global list lock, not a thread-local address that could become invalid
      on detached thread exit. all detached threads, however, must perform a
      second sigprocmask syscall to block implementation-internal signals,
      since locking the thread list with them already blocked is not
      permissible.
      
      the arch-independent C version of __unmapself no longer needs to take
      a lock or setup its own futex address to release the lock, since it
      must necessarily be called with the thread list lock already held,
      guaranteeing exclusive access to the temporary stack.
      
      changes to libc.threads_minus_1 no longer need to be atomic, since
      they are guarded by the thread list lock. it is largely vestigial at
      this point, and can be replaced with a cheaper boolean indicating
      whether the process is multithreaded at some point in the future.
      8f11e612
    • R
      always block signals for starting new threads, refactor start args · 04335d92
      Rich Felker 提交于
      whether signals need to be blocked at thread start, and whether
      unblocking is necessary in the entry point function, has historically
      depended on intricacies of the cancellation design and on whether
      there are scheduling operations to perform on the new thread before
      its successful creation can be committed. future changes to track an
      AS-safe list of live threads will require signals to be blocked
      whenever changes are made to the list, so ...
      
      prior to commits b8742f32 and
      40bae2d3, a signal mask for the entry
      function to restore was part of the pthread structure. it was removed
      to trim down the size of the structure, which both saved a small
      amount of stack space and improved code generation on archs where
      small immediate displacements are less costly than arbitrary ones, by
      limiting the range of offsets between the base of the thread
      structure, its members, and the thread pointer. these commits moved
      the saved mask to a special structure used only when special
      scheduling was needed, in which case the pthread_create caller and new
      thread had to synchronize with each other and could use this memory to
      pass a mask.
      
      this commit partially reverts the above two commits, but instead of
      putting the mask back in the pthread structure, it moves all "start
      argument" members out of the pthread structure, trimming it down
      further, and puts them in a separate structure passed on the new
      thread's stack. the code path for explicit scheduling of the new
      thread is also changed to synchronize with the calling thread in such
      a way to avoid spurious futex wakes.
      04335d92
    • R
      for SIGEV_THREAD timer threads, replace signal handler with sigwaitinfo · 5b74eed3
      Rich Felker 提交于
      this eliminates some ugly hacks that were repurposing the start
      function and start argument fields in the pthread structure for timer
      use, and the need to longjmp out of a signal handler.
      5b74eed3
    • R
      defer free of thread-local dlerror buffers from inconsistent context · aa5a9d15
      Rich Felker 提交于
      __dl_thread_cleanup is called from the context of an exiting thread
      that is not in a consistent state valid for calling application code.
      since commit c9f415d7, it's possible
      (and supported usage) for the allocator to have been replaced by the
      application, so __dl_thread_cleanup can no longer call free. instead,
      reuse the message buffer as a linked-list pointer, and queue it to be
      freed the next time any dynamic linker error message is generated.
      aa5a9d15
  15. 14 2月, 2019 1 次提交
    • R
      fix behavior of gets when input line contains a null byte · b2020571
      Rich Felker 提交于
      the way gets was implemented in terms of fgets, it used the location
      of the null termination to determine where to find and remove the
      newline, if any. an embedded null byte prevented this from working.
      
      this also fixes a one-byte buffer overflow, whereby when gets read an
      N-byte line (not counting newline), it would store two null
      terminators for a total of N+2 bytes. it's unlikely that anyone would
      care that a function whose use is pretty much inherently a buffer
      overflow writes too much, but it could break the only possible correct
      uses of this function, in conjunction with input of known format from
      a trusted/same-privilege-domain source, where the buffer length may
      have been selected to exactly match a line length contract.
      
      there seems to be no correct way to implement gets in terms of a
      single call to fgets or scanf, and using multiple calls would require
      explicit locking, so we might as well just write the logic out
      explicitly character-at-a-time. this isn't fast, but nobody cares if a
      catastrophically unsafe function that's so bad it was removed from the
      C language is fast.
      b2020571
  16. 13 2月, 2019 1 次提交
    • R
      redesign robust mutex states to eliminate data races on type field · 099b89d3
      Rich Felker 提交于
      in order to implement ENOTRECOVERABLE, the implementation has
      traditionally used a bit of the mutex type field to indicate that it's
      recovered after EOWNERDEAD and will go into ENOTRECOVERABLE state if
      pthread_mutex_consistent is not called before unlocking. while it's
      only the thread that holds the lock that needs access to this
      information (except possibly for the sake of pthread_mutex_consistent
      choosing between EINVAL and EPERM for erroneous calls), the change to
      the type field is formally a data race with all other threads that
      perform any operation on the mutex. no individual bits race, and no
      write races are possible, so things are "okay" in some sense, but it's
      still not good.
      
      this patch moves the recovery/consistency state to the mutex
      owner/lock field which is rightfully mutable. bit 30, the same bit the
      kernel uses with a zero owner to indicate that the previous owner died
      holding the lock, is now used with a nonzero owner to indicate that
      the mutex is held but has not yet been marked consistent. note that
      the kernel ABI also reserves bit 29 not to appear in any tid, so the
      sentinel value we use for ENOTRECOVERABLE, 0x7fffffff, does not clash
      with any tid plus bit 30.
      099b89d3
  17. 08 2月, 2019 4 次提交
    • R
      fail fdopendir for O_PATH file descriptors · 042b3ee4
      Rich Felker 提交于
      fdopendir is specified to fail with EBADF if the file descriptor
      passed is not open for reading. while O_PATH is an extension and
      arguably exempt from this requirement, it's used, albeit incompletely,
      to implement O_SEARCH, and fdopendir should fail when passed an
      O_SEARCH file descriptor.
      
      the new check is performed after fstat so that we don't have to
      consider the possibility that the fd is invalid.
      
      an alternate solution would be attempting to pre-fill the buffer using
      getdents, which would fail with EBADF for us, but that seems more
      complex and error-prone and involves either code duplication or
      refactoring, so the simple fix with an additional inexpensive syscall
      is what I've made for now.
      042b3ee4
    • B
      update line discipline constants · 1dd915c3
      Bobby Bingham 提交于
      1dd915c3
    • B
      move arch-invariant definitions out of bits/ioctl.h · 3517d74a
      Bobby Bingham 提交于
      3517d74a
    • A
      locale: ensure dcngettext() preserves errno · 8d829717
      A. Wilcox 提交于
      Some packages call gettext to format a message to be sent to perror.
      If the currently set user locale points to a non-existent .mo file,
      open via __map_file in dcngettext will set errno to ENOENT.
      
      Maintainer's notes: Non-modification of errno is a documented part of
      the interface contract for the GNU version of this function and likely
      other versions. The issue being fixed here seems to be a regression
      from commit 1b52863e, which enabled
      setting of errno from __map_file.
      8d829717
  18. 22 1月, 2019 1 次提交