| 1 |
|
| 2 |
Configuring The Device Cache File Path |
| 3 |
|
| 4 |
Contents |
| 5 |
|
| 6 |
A. Introduction and History |
| 7 |
B. Device Cache File Format |
| 8 |
1. Integrity Checks |
| 9 |
2. The Setgid and Setuid-root States |
| 10 |
C. Device Cache File Path Options |
| 11 |
1. Path Named by ``-D'' |
| 12 |
2. Path Named in Environment Variable |
| 13 |
3. Default System-wide Path |
| 14 |
a. Build Procedure |
| 15 |
4. Default Personal Path |
| 16 |
5. Modified Default Personal Path |
| 17 |
D. Displaying the Default Path |
| 18 |
Appendix A, Unix Dialects Without a Device Cache |
| 19 |
Appendix B, Lsof Dialects and Their Permissions |
| 20 |
1. Setuid-root Lsof Dialects |
| 21 |
2. Setgid Lsof Dialects That Surrender Setgid |
| 22 |
Permission |
| 23 |
|
| 24 |
|
| 25 |
A. Introduction and History |
| 26 |
=========================== |
| 27 |
|
| 28 |
Lsof writes a file of information about the contents of the nodes |
| 29 |
in /dev (or /devices) to reduce its startup overhead on later calls. |
| 30 |
It does this for all Unix dialects, except those noted in Appendix A. |
| 31 |
|
| 32 |
This file, called the device cache file, enables lsof to avoid |
| 33 |
calling the kernel stat(2) function on every node in /dev (or |
| 34 |
/devices) from which it builds a table of correspondence between |
| 35 |
major/minor device numbers and device names. |
| 36 |
|
| 37 |
A full scan of /dev (or /devices) on some systems may involve |
| 38 |
calling the sometimes-slow stat(2) function 10,000 times or more. |
| 39 |
Furthermore, each stat(2) call consumes space in the kernel's name |
| 40 |
cache, forcing from it path name components that would be more |
| 41 |
useful when lsof tries to associate them with open files. |
| 42 |
|
| 43 |
While it's hard to question the usefulness of the device cache, |
| 44 |
it's also hard to decide where it should be written. When the |
| 45 |
feature was first added, the device cache file was written to /tmp, |
| 46 |
and its ownership was set to that of the real user ID (UID) under |
| 47 |
which the creating lsof process was run. However, to enable any |
| 48 |
process to update it when /dev (or /devices) changed, lsof set its |
| 49 |
modes to 0666, thus allowing anyone to read or write it. |
| 50 |
|
| 51 |
The writing of a world-readable and world-writable device cache |
| 52 |
file to any place has security weaknesses. A clever intruder who |
| 53 |
carefully preserves the integrity of the file might be able to |
| 54 |
remove devices that would prevent lsof from observing the intruder's |
| 55 |
files. A clever intruder might also be able to put a symbolic link |
| 56 |
in place and trick lsof into writing to the link's destination with |
| 57 |
its effective permissions, thus bypassing the real user's (possibly |
| 58 |
weaker) permissions. |
| 59 |
|
| 60 |
Later the location of the device cache file was changed. It was |
| 61 |
converted to a personal file, located in the home directory of each |
| 62 |
real UID that executed lsof, and owned by that UID. Thus it was |
| 63 |
no longer possible for one user to affect lsof's access to the |
| 64 |
device cache file, nor was it possible for a user to mount a symbolic |
| 65 |
link attack on a restricted file, but the result was that each lsof |
| 66 |
user had a private copy of the device cache file. |
| 67 |
|
| 68 |
The device cache file feature has undergone some further refinements |
| 69 |
in path name formation to reach its present state. This documentation |
| 70 |
describes the path name formation options open to the lsof builder |
| 71 |
and user after those refinements, and how lsof attempts to insure |
| 72 |
that none of the options presents a security risk. |
| 73 |
|
| 74 |
|
| 75 |
B. Device Cache File Format |
| 76 |
=========================== |
| 77 |
|
| 78 |
The device cache file is a flat file of ASCII text. It has an |
| 79 |
initial statement of how many sections the file might contain -- |
| 80 |
the possible sections are character devices, block devices, clone |
| 81 |
devices, pseudo devices, and checksum. The character devices and |
| 82 |
checksum sections are always present. |
| 83 |
|
| 84 |
Each section has a header that numbers the entries in the section. |
| 85 |
|
| 86 |
The last section is a checksum section that contains a 16 bit cyclic |
| 87 |
redundancy (CRC) checksum of everything in the file but the checksum |
| 88 |
section itself. |
| 89 |
|
| 90 |
Lsof always sets the permission modes of the device cache file to |
| 91 |
0600, and the owner to the real UID of the process that executes |
| 92 |
lsof; the group, the real group ID (GID) of the lsof process. |
| 93 |
|
| 94 |
Setting the permission modes to 0600 means that a system-wide device |
| 95 |
cache file won't be usable unless the procedure that builds it |
| 96 |
changes the modes after lsof has written it. A suitable procedure |
| 97 |
for building a system-wide device cache that shows how to adjust |
| 98 |
these inadequate permission modes is given in the Default System-wide |
| 99 |
Path section. |
| 100 |
|
| 101 |
|
| 102 |
B.1. Integrity Checks |
| 103 |
===================== |
| 104 |
|
| 105 |
When lsof opens the device cache file it makes these integrity |
| 106 |
checks: |
| 107 |
|
| 108 |
1. Lsof must gain permission from access(2) to be able to |
| 109 |
open the file for reading. If lsof is writing the file, |
| 110 |
it usually cedes permission control to the applicable |
| 111 |
directory and file modes and ownerships. (Some additional |
| 112 |
checks apply and they're described in the sections on path |
| 113 |
options.) |
| 114 |
|
| 115 |
By explicit design lsof never writes to the system-wide |
| 116 |
device cache file, even when the real UID of its process |
| 117 |
is root. The system-wide device cache file must be written |
| 118 |
with a root-owned procedure via the ``-D[b|u<path>'' options |
| 119 |
-- i.e., under the system administrator's control. (See |
| 120 |
the Build Procedure sub-section of the Default System-wide |
| 121 |
Path section.) |
| 122 |
|
| 123 |
2. The device cache file's modes must be 0600 (0644 if lsof |
| 124 |
is reading a system-wide device cache file) and its size |
| 125 |
must be non-zero. |
| 126 |
|
| 127 |
3. There must be a correctly formatted section count line |
| 128 |
at the beginning of the file. |
| 129 |
|
| 130 |
4. Each section must have a header line with a count that |
| 131 |
properly numbers the lines in the section. The first words |
| 132 |
of legal section titles are "device", "block", "clone", |
| 133 |
"pseudo", and "CRC". |
| 134 |
|
| 135 |
5. The lines of a section must have the proper format. |
| 136 |
|
| 137 |
6. All lines are included in a 16 bit CRC, and it is recorded |
| 138 |
in a non-checksummed section line at the end of the file. |
| 139 |
|
| 140 |
7. The checksum computed when the file is read must match the |
| 141 |
checksum recorded when the file was written. |
| 142 |
|
| 143 |
8. The checksum section line must be followed by end-of- |
| 144 |
information. |
| 145 |
|
| 146 |
9. Lsof must be able to get matching results from stat(2) |
| 147 |
on a randomly chosen entry of the device section. |
| 148 |
|
| 149 |
|
| 150 |
B.2. The Setgid and Setuid-root States |
| 151 |
====================================== |
| 152 |
|
| 153 |
There are two fundamental ways in which lsof is granted access to |
| 154 |
restricted system resources. Both access methods are related to the |
| 155 |
effective permissions given the lsof binary or executable. |
| 156 |
|
| 157 |
The first and preferable way to grant lsof access to system resources |
| 158 |
through the permissions endowed on its executable is the giving of |
| 159 |
set group ID (setgid) permission. The group is the one that has |
| 160 |
permission to read the kernel memory and swap devices -- e.g., /dev/kmem, |
| 161 |
/dev/mem, /dev/swap, etc. |
| 162 |
|
| 163 |
This method of granting access is called setgid mode because it |
| 164 |
enables lsof to run with an effective group ID set to the one |
| 165 |
granted by the permissions of its executable file and by the group |
| 166 |
that owns the executable file. See the getegid(2) man page for a |
| 167 |
further discussion of effective group ID. |
| 168 |
|
| 169 |
Usually lsof only needs setgid permission to open access to the |
| 170 |
kernel memory files. After they're open, lsof drops its setgid |
| 171 |
permission. |
| 172 |
|
| 173 |
The second and least preferable way to grant lsof access to system |
| 174 |
resources through the permissions endowed on its executable is the |
| 175 |
giving of set user ID to root (setuid-root) permission. This is |
| 176 |
much too strong a permission, but necessary: to use the -X option |
| 177 |
fully for the version of lsof for AIX 5 and above; to use the |
| 178 |
version of lsof for HP-UX 11.11 and above; and to use the version |
| 179 |
of lsof for Linux 2.1.72 and above. These lsof implementations |
| 180 |
require setuid-root permission to be able to access restricted |
| 181 |
resources -- e.g., the individual files of the /proc file system. |
| 182 |
(But note that the setuid-root Linux lsof doesn't need and has no |
| 183 |
device cache support.) |
| 184 |
|
| 185 |
Lsof never drops setuid-root permission, because it needs that |
| 186 |
power throughout its execution. However, when the lsof process is |
| 187 |
setuid-root, lsof disallows these device cache file path options: |
| 188 |
|
| 189 |
1. It ignores the ``-D[b|r|u]<path>'' options. It accepts |
| 190 |
only the ``-Di'' and ``-Dr'' options. |
| 191 |
|
| 192 |
2. It refuses to recognize a path supplied via an environment |
| 193 |
variable. |
| 194 |
|
| 195 |
3. It refuses to accept an additional path component from an |
| 196 |
environment variable to be inserted in the middle of a |
| 197 |
personal device cache file path. |
| 198 |
|
| 199 |
Each restriction is imposed because setuid-root power might allow |
| 200 |
a malicious user to form a device cache file path that would give |
| 201 |
read access to a normally inaccessible place (That's bad enough.), |
| 202 |
or write access to a critical system file (That's the worst case.) |
| 203 |
|
| 204 |
There is one further state that lsof can enter that is slightly |
| 205 |
different from the setuid-root and setgid states. That state occurs |
| 206 |
when lsof is being run from a root shell -- i.e., the lsof real |
| 207 |
user ID is root. To avoid accidental complications, when lsof is |
| 208 |
in this state, it ignores all environment variable options. |
| 209 |
|
| 210 |
In the rest of this document you will find more detailed discussion |
| 211 |
of the special restrictions caused by the type of permission that |
| 212 |
has been given the lsof executable. |
| 213 |
|
| 214 |
|
| 215 |
C. Device Cache File Path Options |
| 216 |
================================= |
| 217 |
|
| 218 |
Lsof offers five options for constructing the path to the device |
| 219 |
cache file. Each has special conditions and safeguards that |
| 220 |
surround its use. The options are: |
| 221 |
|
| 222 |
1. A device cache file that is named in the <path> component |
| 223 |
of the parameters of lsof's ``-D'' option. |
| 224 |
|
| 225 |
========================================================= |
| 226 |
* This is a default option of the lsof distribution. * |
| 227 |
* * |
| 228 |
* Paths specified with this option are read-only unless * |
| 229 |
* the real UID of the lsof process is root (0), or the * |
| 230 |
* lsof process is able to surrender setgid permission * |
| 231 |
* (See Appendix B) and it is not setuid-root. * |
| 232 |
========================================================= |
| 233 |
|
| 234 |
2. A device cache file whose name is specified by an environment |
| 235 |
variable. |
| 236 |
|
| 237 |
========================================================= |
| 238 |
* This is a default option of the lsof distribution. * |
| 239 |
* * |
| 240 |
* This option is enabled when the lsof dialect is able * |
| 241 |
* to surrender setgid permission (See Appendix B.), and * |
| 242 |
* the lsof process is not setuid-root. * |
| 243 |
* * |
| 244 |
* The environment variable path is read-only if the * |
| 245 |
* lsof process does not surrender setgid permission * |
| 246 |
* (See Appendix B.) * |
| 247 |
========================================================= |
| 248 |
|
| 249 |
3. A system-wide default device cache file, located at a path |
| 250 |
determined by the builder of lsof. The lsof builder is also |
| 251 |
responsible for building the device cache file, using a |
| 252 |
different lsof path formation option at a suitable time -- |
| 253 |
e.g., when the system is booted. |
| 254 |
|
| 255 |
========================================================= |
| 256 |
* This is option is disabled by default in the lsof * |
| 257 |
* distribution. * |
| 258 |
* * |
| 259 |
* The path specified with this option is read-only. * |
| 260 |
========================================================= |
| 261 |
|
| 262 |
4. A default personal device cache file, located in the UID's |
| 263 |
home directory. |
| 264 |
|
| 265 |
========================================================= |
| 266 |
* This is a default option of the lsof distribution. * |
| 267 |
========================================================= |
| 268 |
|
| 269 |
5. A personal device cache file whose name is modified by an |
| 270 |
environment variable. |
| 271 |
|
| 272 |
========================================================= |
| 273 |
* This is a default option of the lsof distribution. * |
| 274 |
* * |
| 275 |
* The modified personal path is read-only if the lsof * |
| 276 |
* process does not surrender setgid permission. * |
| 277 |
* * |
| 278 |
* This option is disabled when the lsof process is * |
| 279 |
* setuid-root or its real UID is root (0). * |
| 280 |
========================================================= |
| 281 |
|
| 282 |
When there are multiple choices for the device cache file path, |
| 283 |
lsof chooses from the above list in the order the list is given, |
| 284 |
subject to restrictions based on the effective group and user IDs |
| 285 |
that are in effect. |
| 286 |
|
| 287 |
Each possible path name is discussed in a later section that |
| 288 |
describes the restrictions that apply to it and the method for |
| 289 |
building lsof to use it. |
| 290 |
|
| 291 |
In one special case lsof will use two paths in order. When a |
| 292 |
system-wide device cache file is enabled, and lsof finds that it |
| 293 |
doesn't exist, lsof will attempt to use a personal device cache |
| 294 |
file. |
| 295 |
|
| 296 |
|
| 297 |
C.1. Path Named by ``-D'' |
| 298 |
========================= |
| 299 |
|
| 300 |
The ``-D[b|r|u]<path>'' option can name a path for the device cache |
| 301 |
file where it is unconditionally built (`b'); read, but never |
| 302 |
rebuilt (`r'); and read and rebuilt, if necessary (`u'). |
| 303 |
|
| 304 |
If the lsof process is setuid-root, no path may be specified with |
| 305 |
the ``-D'' option -- i.e., only the `i' function is accepted. The |
| 306 |
`r' option may be used if it doesn't have a path argument. |
| 307 |
|
| 308 |
If the lsof process is not setuid-root, nor is the real UID of the |
| 309 |
lsof process root, a path may accompany the `b', `r', and `u' |
| 310 |
functions if the lsof process surrenders setgid permission. (See |
| 311 |
Appendix B.) If the process doesn't surrender setgid permission, |
| 312 |
then a path may accompany only `r'. |
| 313 |
|
| 314 |
Lsof's permission to access a device cache file at a path specified |
| 315 |
with ``-D[b|r|u]<path>'' depends completely on the permission modes |
| 316 |
and ownerships of the file and its directory components. |
| 317 |
|
| 318 |
When the real UID of the lsof process is root (0), paths may be |
| 319 |
specified with ``-D[b|r|u]''. |
| 320 |
|
| 321 |
==================================================================== |
| 322 |
* * |
| 323 |
* The ``-D[b|r|u]<path>'' option is enabled by default in the lsof * |
| 324 |
* distribution by the following definition in the dialect's * |
| 325 |
* machine.h header file: * |
| 326 |
* * |
| 327 |
* #define HASDCACHE 1 * |
| 328 |
* * |
| 329 |
* To disable all device cache file options, including all ``-D'' * |
| 330 |
* forms, change the above line in the dialect's machine.h file to: * |
| 331 |
* * |
| 332 |
* /* #define HASDCACHE 1 */ * |
| 333 |
* * |
| 334 |
* or remove it. * |
| 335 |
* * |
| 336 |
* The ``-D[b|r|u]<path>'' options are disabled when the lsof * |
| 337 |
* process is setuid-root. If the lsof process isn't setuid-root, * |
| 338 |
* nor is its real UID root (0), and if the lsof process surrenders * |
| 339 |
* setgid permission, ``-D[b|r|u]'' may be accompanied by a path. * |
| 340 |
* * |
| 341 |
* A path may accompany ``-D[b|u]'' when the real UID of the lsof * |
| 342 |
* process is root. * |
| 343 |
* * |
| 344 |
* ``-Dr'' without a path name argument is always acceptable. * |
| 345 |
* * |
| 346 |
==================================================================== |
| 347 |
|
| 348 |
|
| 349 |
C.2. Path Named in Environment Variable |
| 350 |
======================================= |
| 351 |
|
| 352 |
A device cache file path may be declared in an environment variable. |
| 353 |
This option is defined in the dialect's machine.h header file with |
| 354 |
the HASENVDC definition. The value of the HASENVDC definition is |
| 355 |
the environment variable's name. |
| 356 |
|
| 357 |
Lsof will use the value of the environment variable named by HASENVDC |
| 358 |
for the device cache file path unless either of the following |
| 359 |
conditions apply: |
| 360 |
|
| 361 |
1. The lsof process is in the setuid-root state. |
| 362 |
or |
| 363 |
2. The effective and real UIDs of the lsof process are root |
| 364 |
(0). |
| 365 |
|
| 366 |
Lsof uses the value of the HASENVDC environment variable as the |
| 367 |
device cache file path after it senses there is no path declared by |
| 368 |
a ``-D'' option. |
| 369 |
|
| 370 |
A path from an environment variable is read-only unless the lsof |
| 371 |
process surrenders setgid permission. (See Appendix B.) |
| 372 |
|
| 373 |
==================================================================== |
| 374 |
* * |
| 375 |
* The path name environment variable option is enabled by default, * |
| 376 |
* and the environment variable is named LSOFDEVCACHE in the lsof * |
| 377 |
* distribution by the following definition in the dialect's * |
| 378 |
* machine.h header file: * |
| 379 |
* * |
| 380 |
* #define HASENVDC "LSOFDEVCACHE" * |
| 381 |
* * |
| 382 |
* To disable the path name environment variable option, change * |
| 383 |
* the above line in the dialect's machine.h header file to: * |
| 384 |
* * |
| 385 |
* /* #define HASENVDC "LSOFDEVCACHE" */ * |
| 386 |
* * |
| 387 |
* or remove it. To change the name of the environment variable, * |
| 388 |
* change the quoted value of the HASENVDC definition -- e.g., this * |
| 389 |
* form changes the environment variable name to "FOOBAR": * |
| 390 |
* * |
| 391 |
* #define HASENVDC "FOOBAR" * |
| 392 |
* * |
| 393 |
* You can disable the path name environment option by disabling * |
| 394 |
* all device cache file processing when you remove or by disabling * |
| 395 |
* the HASDCACHE definition in the dialect's machine.h header file. * |
| 396 |
* * |
| 397 |
* The path name environment option is disabled when the lsof * |
| 398 |
* process is setuid-root or when the real UID of the lsof process * |
| 399 |
* is root (0). * |
| 400 |
* * |
| 401 |
* The path named in an environment variable is read-only unless * |
| 402 |
* the lsof process surrenders setgid permission. (See Appendix * |
| 403 |
* B.) * |
| 404 |
* * |
| 405 |
==================================================================== |
| 406 |
|
| 407 |
|
| 408 |
C.3. Default System-wide Path |
| 409 |
============================= |
| 410 |
|
| 411 |
When a default system-wide device cache file path is defined (It's |
| 412 |
not enabled by default in the lsof distribution.), lsof will use |
| 413 |
it after it discovers no path has been specified by a ``-D'' option |
| 414 |
and no path has been specified in the environment variable named |
| 415 |
in the string #define HASENVDC of the dialect's machine.h header |
| 416 |
file. |
| 417 |
|
| 418 |
Lsof must be able to open the system-wide device cache file -- |
| 419 |
i.e., it must have read access to the file and search access to |
| 420 |
the directories that lead it. As part of its integrity checks, |
| 421 |
lsof requires that the system-wide device cache file's permission |
| 422 |
modes be 0644. |
| 423 |
|
| 424 |
When lsof discovers that the named system-wide device cache file |
| 425 |
doesn't exist, it will attempt to open a personal device cache file |
| 426 |
should that path formation option be enabled. This is the *only* |
| 427 |
case where lsof will attempt to use two device cache file paths. |
| 428 |
|
| 429 |
The system-wide device cache file is read-only; lsof will never |
| 430 |
attempt to write to it. However, when the real UID of the lsof |
| 431 |
process is root, that process may name the system-wide device |
| 432 |
cache file with ``-D[b|u]<path>''. |
| 433 |
|
| 434 |
==================================================================== |
| 435 |
* * |
| 436 |
* The system-wide file path option is disabled by default in the * |
| 437 |
* lsof distribution. This place-marking definition in a dialect's * |
| 438 |
* machine.h header file may be altered to enable a system-wide * |
| 439 |
* device cache file path: * |
| 440 |
* * |
| 441 |
* /* #define HASSYSDC "/your/choice/of/path" */ * |
| 442 |
* * |
| 443 |
* To enable the system-wide name option, declaring that its path * |
| 444 |
* is ``/foo/bar/lsof.dc'', change the above line in the dialect's * |
| 445 |
* machine.h header file to: * |
| 446 |
* * |
| 447 |
* #define HASSYSDC "/foo/bar/lsof.dc" * |
| 448 |
* * |
| 449 |
* or change the quoted string of the definition to the path of * |
| 450 |
* your choice. * |
| 451 |
* * |
| 452 |
* You can disable the path name environment option by disabling * |
| 453 |
* all device cache file processing when you remove or disable the * |
| 454 |
* HASDCACHE definition in the dialect's machine.h header file. * |
| 455 |
* * |
| 456 |
* The system-wide device cache file is read-only. * |
| 457 |
* * |
| 458 |
==================================================================== |
| 459 |
|
| 460 |
|
| 461 |
C.3.a. Build Procedure |
| 462 |
====================== |
| 463 |
|
| 464 |
The system administrator must build the system-wide device cache |
| 465 |
file at an appropriate time -- e.g., each time the system is booted, |
| 466 |
and each time a node is added, deleted or modified in /dev (or |
| 467 |
/devices). The procedure that builds the system-wide device cache |
| 468 |
file must use lsof's ``-D[b|u]<path>'' options to build the file, |
| 469 |
and must change the file's permission modes to 0644 after it has |
| 470 |
been built. |
| 471 |
|
| 472 |
Here's a simple shell script procedure to build a system-wide device |
| 473 |
cache file. It assumes: |
| 474 |
|
| 475 |
1. The Unix dialect's kernel supports the interpreter script |
| 476 |
execution option -- i.e., a script whose first line has |
| 477 |
the form ``#!<path_to_interpreter>''. |
| 478 |
|
| 479 |
2. The chmod, echo, rm, sh, and test programs are located |
| 480 |
in ``/bin''. |
| 481 |
|
| 482 |
3. The string value of the HASSYSDC definition in the dialect's |
| 483 |
machine.h header file is the path ``/your/choice/of/path''. |
| 484 |
|
| 485 |
4. The lsof executable is located in ``/usr/local/etc''. |
| 486 |
|
| 487 |
#!/bin/sh |
| 488 |
# |
| 489 |
# Simple script to build a system-wide device cache file |
| 490 |
# for lsof. |
| 491 |
|
| 492 |
HASSYSDC=/your/choice/of/path |
| 493 |
/bin/rm -f $HASSYSDC |
| 494 |
/usr/local/etc/lsof -Du$HASSYSDC > /dev/null 2>&1 |
| 495 |
if /bin/test $? -ne 0 |
| 496 |
then |
| 497 |
/bin/echo "WARNING: failed to create $HASSYSDC" |
| 498 |
exit 1 |
| 499 |
fi |
| 500 |
/bin/chmod 0644 $HASSYSDC |
| 501 |
exit 0 |
| 502 |
|
| 503 |
The invocation of lsof uses the ``-Du$HASSYSDC'' option to read |
| 504 |
the device cache file and recreate it if necessary. The invocation |
| 505 |
can be made more efficient if a known process PID -- e.g., ``-p1'' |
| 506 |
-- can be specified. However, if that PID is not always active |
| 507 |
when lsof is called, lsof might set its exit code non-zero, causing |
| 508 |
the subsequent test to believe that the lsof call failed. When in |
| 509 |
doubt, omit the PID specification and accept the extra lsof processing |
| 510 |
time for reporting and discarding all open file information. |
| 511 |
|
| 512 |
|
| 513 |
C.4. Default Personal Path |
| 514 |
========================== |
| 515 |
|
| 516 |
The default personal path option is defined by default in the lsof |
| 517 |
distribution. The path is formed of the home directory of the real |
| 518 |
UID of the lsof process, followed optionally by the contents of |
| 519 |
the HASPERSDCPATH environment variable, followed by ``.lsof_'', |
| 520 |
followed by the first component (characters up to the first period) |
| 521 |
of the name returned by gethostname(2). |
| 522 |
|
| 523 |
If gethostname(2) returns nothing, then nothing will follow the |
| 524 |
``.lsof_'' string. If the first character of what gethostname(2) |
| 525 |
returns is a `.', then all the gethostname(2) value will follow |
| 526 |
the ``/lsof_'' string. (See the ``%l'' conversion for a way to |
| 527 |
make lsof include the entire host name in the path.) |
| 528 |
|
| 529 |
==================================================================== |
| 530 |
* * |
| 531 |
* The personal path option is enabled by default in the lsof * |
| 532 |
* distribution. The HASPERSDC #define in a dialect's machine.h * |
| 533 |
* header is a format specification that tells lsof how to form the * |
| 534 |
* personal device cache file path. The conversions in the format * |
| 535 |
* specification begin with `%' , ala the printf(3) function of the * |
| 536 |
* standard I/O library. These conversions are supported: * |
| 537 |
* * |
| 538 |
* ``%%'' causes a single `%' to appear in the path. * |
| 539 |
* * |
| 540 |
* ``%0'' is a separator that marks the beginning of a path * |
| 541 |
* for a setuid-root lsof process or one whose real * |
| 542 |
* UID is 0. When lsof reaches this conversion and * |
| 543 |
* the process is setuid-root or has a real UID of * |
| 544 |
* root, it erases any previously formed path and * |
| 545 |
* restarts with the next HASPERSDC format character. * |
| 546 |
* If lsof reaches this conversion and the process is * |
| 547 |
* not setuid-root and its real UID is not root, path * |
| 548 |
* formation is ended. * |
| 549 |
* * |
| 550 |
* ``%h'' causes the home directory of the real UID of the * |
| 551 |
* lsof process to appear in the path. * |
| 552 |
* * |
| 553 |
* ``%l'' causes the full name returned by gethostname(2) to * |
| 554 |
* appear in the path. * |
| 555 |
* * |
| 556 |
* ``%L'' causes the first component of the name returned by * |
| 557 |
* gethostname(2) to appear in the path. The first * |
| 558 |
* component is defined to be what appears to the * |
| 559 |
* left of the first `.'. If nothing appears to the * |
| 560 |
* left then everything will appear in the path. * |
| 561 |
* * |
| 562 |
* ``%p'' causes the value of (HASPERSDCPATH) from the * |
| 563 |
* process environment to appear in the path. If the * |
| 564 |
* (HASPERSDCPATH) value doesn't end in a '/', one * |
| 565 |
* will be added. * |
| 566 |
* * |
| 567 |
* ``%u'' causes the login name associated with the real UID * |
| 568 |
* of the lsof process to appear in the path. * |
| 569 |
* * |
| 570 |
* ``%U'' causes the real UID of the lsof process, converted * |
| 571 |
* to a decimal string, to appear in the path. * |
| 572 |
* * |
| 573 |
* All other characters are copied from the format to the * |
| 574 |
* path. CAUTION: THINK VERY CAREFULLY ABOUT THE EFFECT OF * |
| 575 |
* USING CHARACTERS THAT FORM AN ABSOLUTE COMPONENT LIKE * |
| 576 |
* ``/tmp'' IN THE FORMAT. Consider what power your dialect * |
| 577 |
* might have (e.g., if it is setuid-root) when lsof must * |
| 578 |
* create a device cache file at the path. Consider using a * |
| 579 |
* ``%0'' conversion to declare an alternate path for lsof * |
| 580 |
* processes that are setuid-root or whose real uid is root. * |
| 581 |
* See the "How do I put the personal device cache file in * |
| 582 |
* /tmp?" question and answer in 00FAQ for an explanation of * |
| 583 |
* this example: * |
| 584 |
* * |
| 585 |
* #define HASPERSDC "/tmp/.lsof_%u_%l_pers%0%h/.lsof_%L" * |
| 586 |
* * |
| 587 |
* This is the format specification that appears in the machine.h * |
| 588 |
* header files of the lsof distribution: * |
| 589 |
* * |
| 590 |
* #define HASPERSDC "%h/%p.lsof_%L" * |
| 591 |
* * |
| 592 |
* It causes the path to be formed from the home directory of the * |
| 593 |
* real UID of the lsof process (``%h''), followed by `/', followed * |
| 594 |
* by the contents of the environment variable named by * |
| 595 |
* HASPERSDCPATH and a trailing `/', as needed (``%p''), followed * |
| 596 |
* by the string ``.lsof_'', and terminated with the first * |
| 597 |
* component of the host's name (``%L''). * |
| 598 |
* * |
| 599 |
* To change the personal path option, change the HASPERSDC string * |
| 600 |
* and recompile lsof. To disable the personal path option, remove * |
| 601 |
* or disable HASPERSDC. The personal path option is disabled when * |
| 602 |
* HASDCACHE is not defined. * |
| 603 |
* * |
| 604 |
==================================================================== |
| 605 |
|
| 606 |
|
| 607 |
C.5. Modified Default Personal Path |
| 608 |
=================================== |
| 609 |
|
| 610 |
The modified default personal path form is a special case of the |
| 611 |
default personal path. In this form the value of the environment |
| 612 |
variable named by the HASPERSDCPATH #define is inserted in the |
| 613 |
personal path when the ``%p'' conversion appears in the HASPERSDC |
| 614 |
format specification. |
| 615 |
|
| 616 |
This allows, for example, the lsof user to move personal device |
| 617 |
cache files to another branch of the home directory, perhaps to a |
| 618 |
sub-directory where multiple device cache files may appear from |
| 619 |
different machines that use the same NFS- mounted home directory. |
| 620 |
|
| 621 |
The HASPERSDCPATH definition of the dialect's machine.h header file |
| 622 |
names the environment variable. By default in the lsof distribution |
| 623 |
it is LSOFPERSDCPATH. |
| 624 |
|
| 625 |
The modified personal path component is ignored when lsof process |
| 626 |
is setuid-root is root, lest it be maliciously or accidentally used in |
| 627 |
some convoluted form to access paths the real UID cannot. The |
| 628 |
modified personal path component is also ignored when the real UID |
| 629 |
of the lsof process is root (0), so that lsof will not accidentally |
| 630 |
use a personal environment value. |
| 631 |
|
| 632 |
If the lsof process surrenders setgid permission (See Appendix B.), |
| 633 |
lsof can read from and write to the modified personal path. If, |
| 634 |
however, the lsof process doesn't surrender setgid permission, the |
| 635 |
modified personal path is read-only. |
| 636 |
|
| 637 |
If your dialect runs setuid-root or doesn't surrender its setgid |
| 638 |
permission, and you want to use the LSOFPERSDCPATH environment |
| 639 |
variable to address a collection of device cache files in a |
| 640 |
subdirectory, you will have to gather the collection in the |
| 641 |
subdirectory yourself with shell copy or move commands. |
| 642 |
|
| 643 |
==================================================================== |
| 644 |
* * |
| 645 |
* The modified personal path option is enabled by default in the * |
| 646 |
* lsof distribution with these definitions in the dialect's * |
| 647 |
* machine.h header file: * |
| 648 |
* * |
| 649 |
* #define HASPERSDCPATH "LSOFPERSDCPATH" * |
| 650 |
* and * |
| 651 |
* #define HASPERSDC "%h/%p.lsof_%L" * |
| 652 |
* * |
| 653 |
* The value of the definition is the name of the environment * |
| 654 |
* variable that contains the modified personal path name * |
| 655 |
* component that is inserted in the personal path when ``%p'' * |
| 656 |
* appears in HASPERSDC. See the Default Personal Path section * |
| 657 |
* for a complete description of the ``%p'' conversion. * |
| 658 |
* * |
| 659 |
* To disable the modified personal path name component, disable * |
| 660 |
* the HASPERSDCPATH definition in the dialect's machine.h header * |
| 661 |
* file -- e.g., change it to: * |
| 662 |
* * |
| 663 |
* /* #define HASPERSDCPATH "LSOFPERSDCPATH" */ * |
| 664 |
* * |
| 665 |
* or remove the definition altogether. If you do this, don't * |
| 666 |
* forget to remove any ``%p'' conversion from HASPERSDC. * |
| 667 |
* * |
| 668 |
* The modified personal path option is disabled when HASDCACHE is * |
| 669 |
* not defined. * |
| 670 |
* * |
| 671 |
* The modified personal path environment variable value is ignored * |
| 672 |
* when the lsof process is setuid-root or when the real UID of * |
| 673 |
* the lsof process is root (0). * |
| 674 |
* * |
| 675 |
* The modified personal path is read-only when the lsof process * |
| 676 |
* doesn't surrender its setgid permission. * |
| 677 |
* * |
| 678 |
==================================================================== |
| 679 |
|
| 680 |
|
| 681 |
D. Displaying the Default Path |
| 682 |
============================== |
| 683 |
|
| 684 |
Whatever device cache file path formation options you decide to |
| 685 |
use, remember that the lsof help output, displayed in response to |
| 686 |
its ``-h'' or ``-?'' help options, will display the read-mode |
| 687 |
default (the highest numbered) path that lsof has been enabled to |
| 688 |
form from which it will read. |
| 689 |
|
| 690 |
Since some paths are read-only, the path displayed in help option |
| 691 |
output may not be the one to which lsof will write, should that |
| 692 |
become necessary. To see the read-only and write device cache file |
| 693 |
paths, environment variable names, and the personal device cache |
| 694 |
file format specification (HASPERSDC), use the -D? option. |
| 695 |
|
| 696 |
|
| 697 |
Appendix A, Unix Dialects Without a Device Cache |
| 698 |
================================================ |
| 699 |
|
| 700 |
Linux lsof implementations that obtain their information from files |
| 701 |
in the /proc file system do not have device cache support. Generally |
| 702 |
lsof for Linux versions 2.1.72 and greater are /proc based. |
| 703 |
|
| 704 |
|
| 705 |
Appendix B, Lsof Dialects and Their Permissions |
| 706 |
=============================================== |
| 707 |
|
| 708 |
These are the permissions recommended in the lsof distribution. |
| 709 |
|
| 710 |
|
| 711 |
Appendix B.1 Setuid-root Lsof Dialects |
| 712 |
====================================== |
| 713 |
|
| 714 |
These dialect versions of lsof need root permission. For general |
| 715 |
use they may have to be installed setuid-root. |
| 716 |
|
| 717 |
Apple Darwin 9 and Mac OS X 10.[567] |
| 718 |
HP-UX 11.11 and 11.23 |
| 719 |
Linux (no device cache support needed) |
| 720 |
|
| 721 |
|
| 722 |
Appendix B.2 Setgid Lsof Dialects That Surrender Setgid Permission |
| 723 |
================================================================== |
| 724 |
|
| 725 |
Lsof versions for these dialects have WILLDROPGID defined in their |
| 726 |
machine.h header files. |
| 727 |
|
| 728 |
AIX 5.[12] and 5.3-ML1 |
| 729 |
FreeBSD 4.x, 4.1x, 5.x and [6789].x for x86-based systems |
| 730 |
FreeBSD 5.x, [6789].x and 1[012].x for Alpha, AMD64 and Sparc64 |
| 731 |
based systems |
| 732 |
HP-UX 11.00 |
| 733 |
NetBSD 1.[456], 2.x and 3.x for Alpha, x86, and SPARC-based |
| 734 |
systems |
| 735 |
OpenBSD 2.[89] and 3.[0-9] for x86-based systems |
| 736 |
SCO OpenServer Release 5.0.4 for x86-based systems |
| 737 |
SCO|Caldera UnixWare 7.1.4 for x86-based systems |
| 738 |
Solaris 2.6, 8, 9 and 10 |
| 739 |
|
| 740 |
|
| 741 |
Vic Abell <[email protected]> |
| 742 |
February 14, 2018 |