libzypp  17.31.31
CheckAccessDeleted.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <iostream>
13 #include <fstream>
14 #include <unordered_set>
15 #include <iterator>
16 #include <stdio.h>
17 #include <zypp/base/LogControl.h>
18 #include <zypp/base/LogTools.h>
19 #include <zypp/base/String.h>
20 #include <zypp/base/Gettext.h>
21 #include <zypp/base/Exception.h>
22 
23 #include <zypp/PathInfo.h>
24 #include <zypp/ExternalProgram.h>
25 #include <zypp/base/Regex.h>
26 #include <zypp/base/IOStream.h>
27 #include <zypp-core/base/InputStream>
29 
31 
32 using std::endl;
33 
34 #undef ZYPP_BASE_LOGGER_LOGGROUP
35 #define ZYPP_BASE_LOGGER_LOGGROUP "zypp::misc"
36 
38 namespace zypp
39 {
40 
42  namespace
43  {
44  //
45  // lsof output lines are a sequence of NUL terminated fields,
46  // where the 1st char determines the fields type.
47  //
48  // (pcuL) pid command userid loginname
49  // (ftkn).filedescriptor type linkcount filename
50  //
52 
54  typedef std::pair<std::string,std::unordered_set<std::string>> CacheEntry;
55 
62  struct FilterRunsInContainer
63  {
64  private:
65 
66  enum Type {
67  IGNORE,
68  HOST,
69  CONTAINER
70  };
71 
77  Type in_our_root( const Pathname &path ) const {
78 
79  const PathInfo procInfoStat( path );
80 
81  // if we can not stat the file continue to the next one
82  if ( procInfoStat.error() ) return IGNORE;
83 
84  // if the file was unlinked ignore it
85  if ( procInfoStat.nlink() == 0 )
86  return IGNORE;
87 
88  // get the file the link points to, if that fails continue to the next
89  const Pathname linkTarget = filesystem::readlink( path );
90  if ( linkTarget.empty() ) return IGNORE;
91 
92  // Pipe or socket 'type:[inode]' or an 'anon_inode:<file-type>'
93  // They may or may not belong to a container... (bsc#1218291)
94  if ( linkTarget.relative() ) return IGNORE;
95 
96  // get stat info for the target file
97  const PathInfo linkStat( linkTarget );
98 
99  // Non-existent path means it's not reachable by us.
100  if ( !linkStat.isExist() )
101  return CONTAINER;
102 
103  // If the file exists, it could simply mean it exists in and outside a container, check inode to be safe
104  if ( linkStat.ino() != procInfoStat.ino())
105  return CONTAINER;
106 
107  // If the inode is the same, it could simply mean it exists in and outside a container but on different devices, check to be safe
108  if ( linkStat.dev() != procInfoStat.dev() )
109  return CONTAINER;
110 
111  // assume HOST if all tests fail
112  return HOST;
113  }
114 
115  public:
116 
120  bool operator()( const pid_t pid ) const {
121 
122  // first check the exe file
123  const Pathname pidDir = Pathname("/proc") / asString(pid);
124  const Pathname exeFile = pidDir / "exe";
125 
126  auto res = in_our_root( exeFile );
127  if ( res > IGNORE )
128  return res == CONTAINER;
129 
130  // if IGNORE was returned we need to continue testing all the files in /proc/<pid>/map_files until we hopefully
131  // find a still existing file. If all tests fail we will simply assume this pid is running on the HOST
132 
133  // a map of all already tested files, each file can be mapped multiple times and we do not want to check them more than once
134  std::unordered_set<std::string> tested;
135 
136  // iterate over all the entries in /proc/<pid>/map_files
137  filesystem::dirForEach( pidDir / "map_files", [ this, &tested, &res ]( const Pathname & dir_r, const char *const & name_r ){
138 
139  // some helpers to make the code more self explanatory
140  constexpr bool contloop = true;
141  constexpr bool stoploop = false;
142 
143  const Pathname entryName = dir_r / name_r;
144 
145  // get the links target file and check if we alreadys know it, also if we can not read link information we skip the file
146  const Pathname linkTarget = filesystem::readlink( entryName );
147  if ( linkTarget.empty() || !tested.insert( linkTarget.asString() ).second ) return contloop;
148 
149  // try to get file type
150  const auto mappedFileType = in_our_root( entryName );
151 
152  // if we got something, remember the value and stop the loop
153  if ( mappedFileType > IGNORE ) {
154  res = mappedFileType;
155  return stoploop;
156  }
157  return contloop;
158  });
159 
160  // If res is still IGNORE, we did not find a explicit answer. So, to be safe, we assume it is running on the host.
161  if ( res == IGNORE )
162  return false; // can't tell for sure, lets assume host
163 
164  return res == CONTAINER;
165  }
166 
167  FilterRunsInContainer() {}
168  };
169 
170 
176  bool lsofNoOptKi()
177  {
178  using target::rpm::librpmDb;
179  // RpmDb access is blocked while the Target is not initialized.
180  // Launching the Target just for this query would be an overkill.
181  struct TmpUnblock {
182  TmpUnblock()
183  : _wasBlocked( librpmDb::isBlocked() )
184  { if ( _wasBlocked ) librpmDb::unblockAccess(); }
185  ~TmpUnblock()
186  { if ( _wasBlocked ) librpmDb::blockAccess(); }
187  private:
188  bool _wasBlocked;
189  } tmpUnblock;
190 
191  librpmDb::db_const_iterator it;
192  return( it.findPackage( "lsof" ) && it->tag_edition() < Edition("4.90") && !it->tag_provides().count( Capability("backported-option-Ki") ) );
193  }
194 
195  } //namespace
197 
199  {
200  public:
202 
203  bool addDataIf( const CacheEntry & cache_r, std::vector<std::string> *debMap = nullptr );
204  void addCacheIf( CacheEntry & cache_r, const std::string & line_r, std::vector<std::string> *debMap = nullptr );
205 
206  std::map<pid_t,CacheEntry> filterInput( externalprogram::ExternalDataSource &source );
207  CheckAccessDeleted::size_type createProcInfo( const std::map<pid_t,CacheEntry> &in );
208 
209  std::vector<CheckAccessDeleted::ProcInfo> _data;
210  bool _fromLsofFileMode = false; // Set if we currently process data from a debug file
211  bool _verbose = false;
212 
213  std::map<pid_t,std::vector<std::string>> debugMap; //will contain all used lsof files after filtering
215  };
216 
218  {
219  Impl *myClone = new Impl( *this );
220  return myClone;
221  }
222 
227  inline bool CheckAccessDeleted::Impl::addDataIf( const CacheEntry & cache_r, std::vector<std::string> *debMap )
228  {
229  const auto & filelist( cache_r.second );
230 
231  if ( filelist.empty() )
232  return false;
233 
234  // at least one file access so keep it:
235  _data.push_back( CheckAccessDeleted::ProcInfo() );
236  CheckAccessDeleted::ProcInfo & pinfo( _data.back() );
237  pinfo.files.insert( pinfo.files.begin(), filelist.begin(), filelist.end() );
238 
239  const std::string & pline( cache_r.first );
240  std::string commandname; // pinfo.command if still needed...
241  std::ostringstream pLineStr; //rewrite the first line in debug cache
242  for_( ch, pline.begin(), pline.end() )
243  {
244  switch ( *ch )
245  {
246  case 'p':
247  pinfo.pid = &*(ch+1);
248  if ( debMap )
249  pLineStr <<&*(ch)<<'\0';
250  break;
251  case 'R':
252  pinfo.ppid = &*(ch+1);
253  if ( debMap )
254  pLineStr <<&*(ch)<<'\0';
255  break;
256  case 'u':
257  pinfo.puid = &*(ch+1);
258  if ( debMap )
259  pLineStr <<&*(ch)<<'\0';
260  break;
261  case 'L':
262  pinfo.login = &*(ch+1);
263  if ( debMap )
264  pLineStr <<&*(ch)<<'\0';
265  break;
266  case 'c':
267  if ( pinfo.command.empty() ) {
268  commandname = &*(ch+1);
269  // the lsof command name might be truncated, so we prefer /proc/<pid>/exe
270  if (!_fromLsofFileMode)
271  pinfo.command = filesystem::readlink( Pathname("/proc")/pinfo.pid/"exe" ).basename();
272  if ( pinfo.command.empty() )
273  pinfo.command = std::move(commandname);
274  if ( debMap )
275  pLineStr <<'c'<<pinfo.command<<'\0';
276  }
277  break;
278  }
279  if ( *ch == '\n' ) break; // end of data
280  do { ++ch; } while ( *ch != '\0' ); // skip to next field
281  }
282 
283  //replace the data in the debug cache as well
284  if ( debMap ) {
285  pLineStr<<endl;
286  debMap->front() = pLineStr.str();
287  }
288 
289  //entry was added
290  return true;
291  }
292 
293 
299  inline void CheckAccessDeleted::Impl::addCacheIf( CacheEntry & cache_r, const std::string & line_r, std::vector<std::string> *debMap )
300  {
301  const char * f = 0;
302  const char * t = 0;
303  const char * n = 0;
304 
305  for_( ch, line_r.c_str(), ch+line_r.size() )
306  {
307  switch ( *ch )
308  {
309  case 'k':
310  if ( *(ch+1) != '0' ) // skip non-zero link counts
311  return;
312  break;
313  case 'f':
314  f = ch+1;
315  break;
316  case 't':
317  t = ch+1;
318  break;
319  case 'n':
320  n = ch+1;
321  break;
322  }
323  if ( *ch == '\n' ) break; // end of data
324  do { ++ch; } while ( *ch != '\0' ); // skip to next field
325  }
326 
327  if ( !t || !f || !n )
328  return; // wrong filedescriptor/type/name
329 
330  if ( !( ( *t == 'R' && *(t+1) == 'E' && *(t+2) == 'G' && *(t+3) == '\0' )
331  || ( *t == 'D' && *(t+1) == 'E' && *(t+2) == 'L' && *(t+3) == '\0' ) ) )
332  return; // wrong type
333 
334  if ( !( ( *f == 'm' && *(f+1) == 'e' && *(f+2) == 'm' && *(f+3) == '\0' )
335  || ( *f == 't' && *(f+1) == 'x' && *(f+2) == 't' && *(f+3) == '\0' )
336  || ( *f == 'D' && *(f+1) == 'E' && *(f+2) == 'L' && *(f+3) == '\0' )
337  || ( *f == 'l' && *(f+1) == 't' && *(f+2) == 'x' && *(f+3) == '\0' ) ) )
338  return; // wrong filedescriptor type
339 
340  if ( str::contains( n, "(stat: Permission denied)" ) )
341  return; // Avoid reporting false positive due to insufficient permission.
342 
343  if ( ! _verbose )
344  {
345  if ( ! ( str::contains( n, "/lib" ) || str::contains( n, "bin/" ) ) )
346  return; // Try to avoid reporting false positive unless verbose.
347  }
348 
349  if ( *f == 'm' || *f == 'D' ) // skip some wellknown nonlibrary memorymapped files
350  {
351  static const char * black[] = {
352  "/SYSV"
353  , "/var/"
354  , "/dev/"
355  , "/tmp/"
356  , "/proc/"
357  , "/memfd:"
358  };
359  for_( it, arrayBegin( black ), arrayEnd( black ) )
360  {
361  if ( str::hasPrefix( n, *it ) )
362  return;
363  }
364  }
365  // Add if no duplicate
366  if ( debMap && cache_r.second.find(n) == cache_r.second.end() ) {
367  debMap->push_back(line_r);
368  }
369  cache_r.second.insert( n );
370  }
371 
373  : _pimpl(new Impl)
374  {
375  if ( doCheck_r ) check();
376  }
377 
378  CheckAccessDeleted::size_type CheckAccessDeleted::check( const Pathname &lsofOutput_r, bool verbose_r )
379  {
380  _pimpl->_verbose = verbose_r;
381  _pimpl->_fromLsofFileMode = true;
382 
383  FILE *inFile = fopen( lsofOutput_r.c_str(), "r" );
384  if ( !inFile ) {
385  ZYPP_THROW( Exception( str::Format("Opening input file %1% failed.") % lsofOutput_r.c_str() ) );
386  }
387 
388  //inFile is closed by ExternalDataSource
389  externalprogram::ExternalDataSource inSource( inFile, nullptr );
390  auto cache = _pimpl->filterInput( inSource );
391  return _pimpl->createProcInfo( cache );
392  }
393 
395  {
396  // cachemap: PID => (deleted files)
397  // NOTE: omit PIDs running in a (lxc/docker) container
398  std::map<pid_t,CacheEntry> cachemap;
399 
400  bool debugEnabled = !_debugFile.empty();
401 
402  pid_t cachepid = 0;
403  FilterRunsInContainer runsInLXC;
404  MIL << "Silently scanning lsof output..." << endl;
405  zypp::base::LogControl::TmpLineWriter shutUp; // suppress excessive readdir etc. logging in runsInLXC
406  for( std::string line = source.receiveLine( 30 * 1000 ); ! line.empty(); line = source.receiveLine( 30 * 1000 ) )
407  {
408  // NOTE: line contains '\0' separeated fields!
409  if ( line[0] == 'p' )
410  {
411  str::strtonum( line.c_str()+1, cachepid ); // line is "p<PID>\0...."
412  if ( _fromLsofFileMode || !runsInLXC( cachepid ) ) {
413  if ( debugEnabled ) {
414  auto &pidMad = debugMap[cachepid];
415  if ( pidMad.empty() )
416  debugMap[cachepid].push_back( line );
417  else
418  debugMap[cachepid].front() = line;
419  }
420  cachemap[cachepid].first.swap( line );
421  } else {
422  cachepid = 0; // ignore this pid
423  }
424  }
425  else if ( cachepid )
426  {
427  auto &dbgMap = debugMap[cachepid];
428  addCacheIf( cachemap[cachepid], line, debugEnabled ? &dbgMap : nullptr);
429  }
430  }
431  return cachemap;
432  }
433 
435  {
436  static const char* argv[] = { "lsof", "-n", "-FpcuLRftkn0", "-K", "i", NULL };
437  if ( lsofNoOptKi() )
438  argv[3] = NULL;
439 
440  _pimpl->_verbose = verbose_r;
441  _pimpl->_fromLsofFileMode = false;
442 
444  std::map<pid_t,CacheEntry> cachemap;
445 
446  try {
447  cachemap = _pimpl->filterInput( prog );
448  } catch ( const io::TimeoutException &e ) {
449  ZYPP_CAUGHT( e );
450  prog.kill();
451  ZYPP_THROW ( Exception( "Reading data from 'lsof' timed out.") );
452  }
453 
454  int ret = prog.close();
455  if ( ret != 0 )
456  {
457  if ( ret == 129 )
458  {
459  ZYPP_THROW( Exception(_("Please install package 'lsof' first.") ) );
460  }
461  Exception err( str::Format("Executing 'lsof' failed (%1%).") % ret );
462  err.remember( prog.execError() );
463  ZYPP_THROW( err );
464  }
465 
466  return _pimpl->createProcInfo( cachemap );
467  }
468 
470  {
471  std::ofstream debugFileOut;
472  bool debugEnabled = false;
473  if ( !_debugFile.empty() ) {
474  debugFileOut.open( _debugFile.c_str() );
475  debugEnabled = debugFileOut.is_open();
476 
477  if ( !debugEnabled ) {
478  ERR<<"Unable to open debug file: "<<_debugFile<<endl;
479  }
480  }
481 
482  _data.clear();
483  for ( const auto &cached : in )
484  {
485  if (!debugEnabled)
486  addDataIf( cached.second);
487  else {
488  std::vector<std::string> *mapPtr = nullptr;
489 
490  auto dbgInfo = debugMap.find(cached.first);
491  if ( dbgInfo != debugMap.end() )
492  mapPtr = &(dbgInfo->second);
493 
494  if( !addDataIf( cached.second, mapPtr ) )
495  continue;
496 
497  for ( const std::string &dbgLine: dbgInfo->second ) {
498  debugFileOut.write( dbgLine.c_str(), dbgLine.length() );
499  }
500  }
501  }
502  return _data.size();
503  }
504 
506  {
507  return _pimpl->_data.empty();
508  }
509 
511  {
512  return _pimpl->_data.size();
513  }
514 
516  {
517  return _pimpl->_data.begin();
518  }
519 
521  {
522  return _pimpl->_data.end();
523  }
524 
526  {
527  _pimpl->_debugFile = filename_r;
528  }
529 
530  std::string CheckAccessDeleted::findService( pid_t pid_r )
531  {
532  ProcInfo p;
533  p.pid = str::numstring( pid_r );
534  return p.service();
535  }
536 
538  {
539  // cgroup entries like:
540  // 1:name=systemd:/system.slice/systemd-udevd.service
541  // 0::/system.slice/systemd-udevd.service
542  // 0::/system.slice/systemd-udevd.service/udev
543  static const str::regex rx( "(0::|[0-9]+:name=systemd:)/system.slice/(.*/)?(.*).service(/.*)?$" );
544  str::smatch what;
545  std::string ret;
546  iostr::simpleParseFile( InputStream( Pathname("/proc")/pid/"cgroup" ),
547  [&]( int num_r, std::string line_r )->bool
548  {
549  if ( str::regex_match( line_r, what, rx ) )
550  {
551  ret = what[3];
552  return false; // stop after match
553  }
554  return true;
555  } );
556  return ret;
557  }
558 
559  /******************************************************************
560  **
561  ** FUNCTION NAME : operator<<
562  ** FUNCTION TYPE : std::ostream &
563  */
564  std::ostream & operator<<( std::ostream & str, const CheckAccessDeleted & obj )
565  {
566  return dumpRange( str << "CheckAccessDeleted ",
567  obj.begin(),
568  obj.end() );
569  }
570 
571  /******************************************************************
572  **
573  ** FUNCTION NAME : operator<<
574  ** FUNCTION TYPE : std::ostream &
575  */
576  std::ostream & operator<<( std::ostream & str, const CheckAccessDeleted::ProcInfo & obj )
577  {
578  if ( obj.pid.empty() )
579  return str << "<NoProc>";
580 
581  return dumpRangeLine( str << obj.command
582  << '<' << obj.pid
583  << '|' << obj.ppid
584  << '|' << obj.puid
585  << '|' << obj.login
586  << '>',
587  obj.files.begin(),
588  obj.files.end() );
589  }
590 
592 } // namespace zypp
std::string asString(const Patch::Category &obj)
Definition: Patch.cc:122
bool addDataIf(const CacheEntry &cache_r, std::vector< std::string > *debMap=nullptr)
Add cache to data if the process is accessing deleted files.
Data about one running process accessing deleted files.
#define MIL
Definition: Logger.h:96
Bidirectional stream to external data.
bool contains(const C_Str &str_r, const C_Str &val_r)
Locate substring case sensitive.
Definition: String.h:991
std::map< pid_t, CacheEntry > filterInput(externalprogram::ExternalDataSource &source)
#define _(MSG)
Definition: Gettext.h:37
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:428
Regular expression.
Definition: Regex.h:94
bool kill()
Kill the program.
std::map< pid_t, std::vector< std::string > > debugMap
int readlink(const Pathname &symlink_r, Pathname &target_r)
Like &#39;readlink&#39;.
Definition: PathInfo.cc:924
void setDebugOutputFile(const Pathname &filename_r)
Writes all filtered process entries that make it into the final set into a file specified by filename...
const std::string & execError() const
Some detail telling why the execution failed, if it failed.
std::ostream & dumpRange(std::ostream &str, TIterator begin, TIterator end, const std::string &intro="{", const std::string &pfx="\ ", const std::string &sep="\ ", const std::string &sfx="\, const std::string &extro="}")
Print range defined by iterators (multiline style).
Definition: LogTools.h:107
int dirForEach(const Pathname &dir_r, const StrMatcher &matcher_r, function< bool(const Pathname &, const char *const)> fnc_r)
Definition: PathInfo.cc:32
#define for_(IT, BEG, END)
Convenient for-loops using iterator.
Definition: Easy.h:28
const char * c_str() const
String representation.
Definition: Pathname.h:110
std::string command
process command name
String related utilities and Regular expression matching.
Helper to create and pass std::istream.
Definition: inputstream.h:56
std::string receiveLine()
Read one line from the input stream.
Convenient building of std::string with boost::format.
Definition: String.h:252
Exchange LineWriter for the lifetime of this object.
Definition: LogControl.h:190
std::ostream & operator<<(std::ostream &str, const CheckAccessDeleted &obj)
#define ERR
Definition: Logger.h:98
void remember(const Exception &old_r)
Store an other Exception as history.
Definition: Exception.cc:105
CheckAccessDeleted::Impl * clone() const
bool empty() const
Test for an empty path.
Definition: Pathname.h:114
size_type check(bool verbose_r=false)
Check for running processes which access deleted executables or libraries.
RWCOW_pointer< Impl > _pimpl
const_iterator begin() const
int simpleParseFile(std::istream &str_r, ParseFlags flags_r, function< bool(int, std::string)> consume_r)
Simple lineparser optionally trimming and skipping comments.
Definition: IOStream.cc:124
Execute a program and give access to its io An object of this class encapsulates the execution of an ...
std::list< PublicKeyData > _data
Definition: KeyRing.cc:191
std::vector< CheckAccessDeleted::ProcInfo > _data
TInt strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:388
std::string puid
process user ID
const_iterator end() const
std::string numstring(char n, int w=0)
Definition: String.h:289
CheckAccessDeleted::size_type createProcInfo(const std::map< pid_t, CacheEntry > &in)
#define arrayEnd(A)
Definition: Easy.h:43
int close()
Wait for the progamm to complete.
#define ZYPP_CAUGHT(EXCPT)
Drops a logline telling the Exception was caught (in order to handle it).
Definition: Exception.h:436
Regular expression match result.
Definition: Regex.h:167
Base class for Exception.
Definition: Exception.h:145
Check for running processes which access deleted executables or libraries.
CheckAccessDeleted(bool doCheck_r=true)
Default ctor performs check immediately.
std::ostream & dumpRangeLine(std::ostream &str, TIterator begin, TIterator end)
Print range defined by iterators (single line style).
Definition: LogTools.h:130
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
bool hasPrefix(const C_Str &str_r, const C_Str &prefix_r)
Return whether str_r has prefix prefix_r.
Definition: String.h:1027
std::vector< ProcInfo >::const_iterator const_iterator
std::string login
process login name
void addCacheIf(CacheEntry &cache_r, const std::string &line_r, std::vector< std::string > *debMap=nullptr)
Add file to cache if it refers to a deleted executable or library file:
std::vector< std::string > files
list of deleted executables or libraries accessed
#define arrayBegin(A)
Simple C-array iterator.
Definition: Easy.h:41
std::string ppid
parent process ID
std::string service() const
Guess if command was started by a systemd service script.
static std::string findService(pid_t pid_r)
Guess if pid was started by a systemd service script.