(root)/
strace-6.5/
src/
open.c
       1  /*
       2   * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
       3   * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
       4   * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
       5   * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
       6   * Copyright (c) 2005-2007 Roland McGrath <roland@redhat.com>
       7   * Copyright (c) 2006-2007 Ulrich Drepper <drepper@redhat.com>
       8   * Copyright (c) 2009-2013 Denys Vlasenko <dvlasenk@redhat.com>
       9   * Copyright (c) 2005-2015 Dmitry V. Levin <ldv@strace.io>
      10   * Copyright (c) 2014-2022 The strace developers.
      11   * All rights reserved.
      12   *
      13   * SPDX-License-Identifier: LGPL-2.1-or-later
      14   */
      15  
      16  #include "defs.h"
      17  #include "xstring.h"
      18  #include "kernel_fcntl.h"
      19  #include "number_set.h"
      20  #include <linux/openat2.h>
      21  #include <linux/fcntl.h>
      22  
      23  #include "xlat/open_access_modes.h"
      24  #include "xlat/open_mode_flags.h"
      25  #include "xlat/open_resolve_flags.h"
      26  
      27  /* The fd is an "int", so when decoding x86 on x86_64, we need to force sign
      28   * extension to get the right value.  We do this by declaring fd as int here.
      29   */
      30  void
      31  print_dirfd(struct tcb *tcp, int fd)
      32  {
      33  	if (fd == AT_FDCWD) {
      34  		print_xlat_d(AT_FDCWD);
      35  
      36  		if (!is_number_in_set(DECODE_FD_PATH, decode_fd_set))
      37  			goto done;
      38  
      39  		int proc_pid = get_proc_pid(tcp->pid);
      40  		if (!proc_pid)
      41  			goto done;
      42  
      43  		static const char cwd_path[] = "/proc/%u/cwd";
      44  		char linkpath[sizeof(cwd_path) + sizeof(int) * 3];
      45  		xsprintf(linkpath, cwd_path, proc_pid);
      46  
      47  		char buf[PATH_MAX];
      48  		ssize_t n = readlink(linkpath, buf, sizeof(buf));
      49  		if ((size_t) n >= sizeof(buf))
      50  			goto done;
      51  
      52  		tprint_associated_info_begin();
      53  		print_quoted_string_ex(buf, n,
      54  				       QUOTE_OMIT_LEADING_TRAILING_QUOTES,
      55  				       "<>");
      56  		tprint_associated_info_end();
      57  done:		;
      58  	} else {
      59  		printfd(tcp, fd);
      60  	}
      61  #ifdef ENABLE_SECONTEXT
      62  	tcp->last_dirfd = fd;
      63  #endif
      64  }
      65  
      66  /*
      67   * low bits of the open(2) flags define access mode,
      68   * other bits are real flags.
      69   */
      70  static const char *
      71  sprint_open_modes64(uint64_t flags)
      72  {
      73  	static char outstr[sizeof("flags O_ACCMODE")];
      74  	char *p;
      75  	char sep;
      76  	const char *str;
      77  
      78  	sep = ' ';
      79  	p = stpcpy(outstr, "flags");
      80  	str = xlookup(open_access_modes, flags & 3);
      81  	if (str) {
      82  		*p++ = sep;
      83  		p = stpcpy(p, str);
      84  		flags &= ~3;
      85  		if (!flags)
      86  			return outstr;
      87  		sep = '|';
      88  	}
      89  	*p = '\0';
      90  
      91  	return sprintflags_ex(outstr, open_mode_flags, flags, sep,
      92  			      XLAT_STYLE_ABBREV) ?: outstr;
      93  }
      94  
      95  const char *
      96  sprint_open_modes(unsigned int flags)
      97  {
      98  	return sprint_open_modes64(flags);
      99  }
     100  
     101  static void
     102  tprint_open_modes64(uint64_t flags)
     103  {
     104  	print_xlat_ex(flags, sprint_open_modes64(flags) + sizeof("flags"),
     105  		      XLAT_STYLE_DEFAULT);
     106  }
     107  void
     108  tprint_open_modes(unsigned int flags)
     109  {
     110  	tprint_open_modes64(flags);
     111  }
     112  
     113  static int
     114  decode_open(struct tcb *tcp, int offset)
     115  {
     116  	/* pathname */
     117  	printpath(tcp, tcp->u_arg[offset]);
     118  	tprint_arg_next();
     119  
     120  	/* flags */
     121  	tprint_open_modes(tcp->u_arg[offset + 1]);
     122  
     123  	if (tcp->u_arg[offset + 1] & (O_CREAT | __O_TMPFILE)) {
     124  		tprint_arg_next();
     125  
     126  		/* mode */
     127  		print_numeric_umode_t(tcp->u_arg[offset + 2]);
     128  	}
     129  
     130  	return RVAL_DECODED | RVAL_FD;
     131  }
     132  
     133  SYS_FUNC(open)
     134  {
     135  	return decode_open(tcp, 0);
     136  }
     137  
     138  static void
     139  print_open_how(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t size)
     140  {
     141  	enum { OPEN_HOW_MIN_SIZE = 24 };
     142  
     143  	struct open_how how;
     144  
     145  	if (size < OPEN_HOW_MIN_SIZE) {
     146  		printaddr(addr);
     147  		return;
     148  	}
     149  	if (umoven_or_printaddr(tcp, addr, MIN(size, sizeof(how)), &how))
     150  		return;
     151  
     152  	tprint_struct_begin();
     153  	PRINT_FIELD_OBJ_VAL(how, flags, tprint_open_modes64);
     154  	if ((how.flags & (O_CREAT | __O_TMPFILE)) || how.mode) {
     155  		tprint_struct_next();
     156  		PRINT_FIELD_OBJ_U(how, mode, print_numeric_ll_umode_t);
     157  	}
     158  	tprint_struct_next();
     159  	PRINT_FIELD_FLAGS(how, resolve, open_resolve_flags, "RESOLVE_???");
     160  
     161  	if (size > sizeof(how)) {
     162  		print_nonzero_bytes(tcp, tprint_struct_next, addr, sizeof(how),
     163  				    MIN(size, get_pagesize()), QUOTE_FORCE_HEX);
     164  	}
     165  
     166  	tprint_struct_end();
     167  }
     168  
     169  SYS_FUNC(openat)
     170  {
     171  	/* dirfd */
     172  	print_dirfd(tcp, tcp->u_arg[0]);
     173  	tprint_arg_next();
     174  
     175  	return decode_open(tcp, 1);
     176  }
     177  
     178  SYS_FUNC(openat2)
     179  {
     180  	/* dirfd */
     181  	print_dirfd(tcp, tcp->u_arg[0]);
     182  	tprint_arg_next();
     183  
     184  	/* pathname */
     185  	printpath(tcp, tcp->u_arg[1]);
     186  	tprint_arg_next();
     187  
     188  	/* how */
     189  	print_open_how(tcp, tcp->u_arg[2], tcp->u_arg[3]);
     190  	tprint_arg_next();
     191  
     192  	/* size */
     193  	PRINT_VAL_U(tcp->u_arg[3]);
     194  
     195  	return RVAL_DECODED | RVAL_FD;
     196  }
     197  
     198  SYS_FUNC(creat)
     199  {
     200  	/* pathname */
     201  	printpath(tcp, tcp->u_arg[0]);
     202  	tprint_arg_next();
     203  
     204  	/* mode */
     205  	print_numeric_umode_t(tcp->u_arg[1]);
     206  
     207  	return RVAL_DECODED | RVAL_FD;
     208  }