1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #include <linux/fsnotify_backend.h> 3 #include <linux/path.h> 4 #include <linux/slab.h> 5 #include <linux/string.h> 6 #include <linux/exportfs.h> 7 #include <linux/hashtable.h> 8 9 extern struct kmem_cache *fanotify_mark_cache; 10 extern struct kmem_cache *fanotify_fid_event_cachep; 11 extern struct kmem_cache *fanotify_path_event_cachep; 12 extern struct kmem_cache *fanotify_perm_event_cachep; 13 extern struct kmem_cache *fanotify_mnt_event_cachep; 14 15 /* Possible states of the permission event */ 16 enum { 17 FAN_EVENT_INIT, 18 FAN_EVENT_REPORTED, 19 FAN_EVENT_ANSWERED, 20 FAN_EVENT_CANCELED, 21 }; 22 23 /* 24 * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation). 25 * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is 26 * stored in either the first or last 2 dwords. 27 */ 28 #define FANOTIFY_INLINE_FH_LEN (3 << 2) 29 #define FANOTIFY_FH_HDR_LEN sizeof(struct fanotify_fh) 30 31 /* Fixed size struct for file handle */ 32 struct fanotify_fh { 33 u8 type; 34 u8 len; 35 #define FANOTIFY_FH_FLAG_EXT_BUF 1 36 u8 flags; 37 u8 pad; 38 } __aligned(4); 39 40 /* Variable size struct for dir file handle + child file handle + name */ 41 struct fanotify_info { 42 /* size of dir_fh/file_fh including fanotify_fh hdr size */ 43 u8 dir_fh_totlen; 44 u8 dir2_fh_totlen; 45 u8 file_fh_totlen; 46 u8 pad; 47 /* 48 * Deliberately not limited to NAME_MAX: some filesystems return longer 49 * names (vfat up to 255 UTF-16 characters, as Windows allows, which is 50 * up to 765 bytes of UTF-8; FUSE up to PATH_MAX - 1), and readdir() and 51 * inotify report them in full. Only names of PATH_MAX bytes or more, 52 * which no path can refer to, are dropped. 53 */ 54 u16 name_len; 55 u16 name2_len; 56 unsigned char buf[]; 57 /* 58 * (struct fanotify_fh) dir_fh starts at buf[0] 59 * (optional) dir2_fh starts at buf[dir_fh_totlen] 60 * (optional) file_fh starts at buf[dir_fh_totlen + dir2_fh_totlen] 61 * name starts at buf[dir_fh_totlen + dir2_fh_totlen + file_fh_totlen] 62 * ... 63 */ 64 #define FANOTIFY_DIR_FH_SIZE(info) ((info)->dir_fh_totlen) 65 #define FANOTIFY_DIR2_FH_SIZE(info) ((info)->dir2_fh_totlen) 66 #define FANOTIFY_FILE_FH_SIZE(info) ((info)->file_fh_totlen) 67 #define FANOTIFY_NAME_SIZE(info) ((info)->name_len + 1) 68 #define FANOTIFY_NAME2_SIZE(info) ((info)->name2_len + 1) 69 70 #define FANOTIFY_DIR_FH_OFFSET(info) 0 71 #define FANOTIFY_DIR2_FH_OFFSET(info) \ 72 (FANOTIFY_DIR_FH_OFFSET(info) + FANOTIFY_DIR_FH_SIZE(info)) 73 #define FANOTIFY_FILE_FH_OFFSET(info) \ 74 (FANOTIFY_DIR2_FH_OFFSET(info) + FANOTIFY_DIR2_FH_SIZE(info)) 75 #define FANOTIFY_NAME_OFFSET(info) \ 76 (FANOTIFY_FILE_FH_OFFSET(info) + FANOTIFY_FILE_FH_SIZE(info)) 77 #define FANOTIFY_NAME2_OFFSET(info) \ 78 (FANOTIFY_NAME_OFFSET(info) + FANOTIFY_NAME_SIZE(info)) 79 80 #define FANOTIFY_DIR_FH_BUF(info) \ 81 ((info)->buf + FANOTIFY_DIR_FH_OFFSET(info)) 82 #define FANOTIFY_DIR2_FH_BUF(info) \ 83 ((info)->buf + FANOTIFY_DIR2_FH_OFFSET(info)) 84 #define FANOTIFY_FILE_FH_BUF(info) \ 85 ((info)->buf + FANOTIFY_FILE_FH_OFFSET(info)) 86 #define FANOTIFY_NAME_BUF(info) \ 87 ((info)->buf + FANOTIFY_NAME_OFFSET(info)) 88 #define FANOTIFY_NAME2_BUF(info) \ 89 ((info)->buf + FANOTIFY_NAME2_OFFSET(info)) 90 } __aligned(4); 91 92 static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh) 93 { 94 return (fh->flags & FANOTIFY_FH_FLAG_EXT_BUF); 95 } 96 97 static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh) 98 { 99 BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN % 4); 100 BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) > 101 FANOTIFY_INLINE_FH_LEN); 102 return (char **)ALIGN((unsigned long)(fh + 1), __alignof__(char *)); 103 } 104 105 static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh) 106 { 107 return *fanotify_fh_ext_buf_ptr(fh); 108 } 109 110 static inline void *fanotify_fh_buf(struct fanotify_fh *fh) 111 { 112 return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh + 1; 113 } 114 115 static inline int fanotify_info_dir_fh_len(struct fanotify_info *info) 116 { 117 if (!info->dir_fh_totlen || 118 WARN_ON_ONCE(info->dir_fh_totlen < FANOTIFY_FH_HDR_LEN)) 119 return 0; 120 121 return info->dir_fh_totlen - FANOTIFY_FH_HDR_LEN; 122 } 123 124 static inline struct fanotify_fh *fanotify_info_dir_fh(struct fanotify_info *info) 125 { 126 BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4); 127 128 return (struct fanotify_fh *)FANOTIFY_DIR_FH_BUF(info); 129 } 130 131 static inline int fanotify_info_dir2_fh_len(struct fanotify_info *info) 132 { 133 if (!info->dir2_fh_totlen || 134 WARN_ON_ONCE(info->dir2_fh_totlen < FANOTIFY_FH_HDR_LEN)) 135 return 0; 136 137 return info->dir2_fh_totlen - FANOTIFY_FH_HDR_LEN; 138 } 139 140 static inline struct fanotify_fh *fanotify_info_dir2_fh(struct fanotify_info *info) 141 { 142 return (struct fanotify_fh *)FANOTIFY_DIR2_FH_BUF(info); 143 } 144 145 static inline int fanotify_info_file_fh_len(struct fanotify_info *info) 146 { 147 if (!info->file_fh_totlen || 148 WARN_ON_ONCE(info->file_fh_totlen < FANOTIFY_FH_HDR_LEN)) 149 return 0; 150 151 return info->file_fh_totlen - FANOTIFY_FH_HDR_LEN; 152 } 153 154 static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info) 155 { 156 return (struct fanotify_fh *)FANOTIFY_FILE_FH_BUF(info); 157 } 158 159 static inline char *fanotify_info_name(struct fanotify_info *info) 160 { 161 if (!info->name_len) 162 return NULL; 163 164 return FANOTIFY_NAME_BUF(info); 165 } 166 167 static inline char *fanotify_info_name2(struct fanotify_info *info) 168 { 169 if (!info->name2_len) 170 return NULL; 171 172 return FANOTIFY_NAME2_BUF(info); 173 } 174 175 static inline void fanotify_info_init(struct fanotify_info *info) 176 { 177 BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN + MAX_HANDLE_SZ > U8_MAX); 178 BUILD_BUG_ON(PATH_MAX > U16_MAX); 179 180 info->dir_fh_totlen = 0; 181 info->dir2_fh_totlen = 0; 182 info->file_fh_totlen = 0; 183 info->name_len = 0; 184 info->name2_len = 0; 185 } 186 187 /* These set/copy helpers MUST be called by order */ 188 static inline void fanotify_info_set_dir_fh(struct fanotify_info *info, 189 unsigned int totlen) 190 { 191 if (WARN_ON_ONCE(info->dir2_fh_totlen > 0) || 192 WARN_ON_ONCE(info->file_fh_totlen > 0) || 193 WARN_ON_ONCE(info->name_len > 0) || 194 WARN_ON_ONCE(info->name2_len > 0)) 195 return; 196 197 info->dir_fh_totlen = totlen; 198 } 199 200 static inline void fanotify_info_set_dir2_fh(struct fanotify_info *info, 201 unsigned int totlen) 202 { 203 if (WARN_ON_ONCE(info->file_fh_totlen > 0) || 204 WARN_ON_ONCE(info->name_len > 0) || 205 WARN_ON_ONCE(info->name2_len > 0)) 206 return; 207 208 info->dir2_fh_totlen = totlen; 209 } 210 211 static inline void fanotify_info_set_file_fh(struct fanotify_info *info, 212 unsigned int totlen) 213 { 214 if (WARN_ON_ONCE(info->name_len > 0) || 215 WARN_ON_ONCE(info->name2_len > 0)) 216 return; 217 218 info->file_fh_totlen = totlen; 219 } 220 221 static inline void fanotify_info_copy_name(struct fanotify_info *info, 222 const struct qstr *name) 223 { 224 if (WARN_ON_ONCE(info->name2_len > 0)) 225 return; 226 227 info->name_len = name->len; 228 strscpy(fanotify_info_name(info), name->name, name->len + 1); 229 } 230 231 static inline void fanotify_info_copy_name2(struct fanotify_info *info, 232 const struct qstr *name) 233 { 234 info->name2_len = name->len; 235 strscpy(fanotify_info_name2(info), name->name, name->len + 1); 236 } 237 238 /* 239 * Common structure for fanotify events. Concrete structs are allocated in 240 * fanotify_handle_event() and freed when the information is retrieved by 241 * userspace. The type of event determines how it was allocated, how it will 242 * be freed and which concrete struct it may be cast to. 243 */ 244 enum fanotify_event_type { 245 FANOTIFY_EVENT_TYPE_FID, /* fixed length */ 246 FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */ 247 FANOTIFY_EVENT_TYPE_PATH, 248 FANOTIFY_EVENT_TYPE_PATH_PERM, 249 FANOTIFY_EVENT_TYPE_OVERFLOW, /* struct fanotify_event */ 250 FANOTIFY_EVENT_TYPE_FS_ERROR, /* struct fanotify_error_event */ 251 FANOTIFY_EVENT_TYPE_MNT, 252 __FANOTIFY_EVENT_TYPE_NUM 253 }; 254 255 #define FANOTIFY_EVENT_TYPE_BITS \ 256 (ilog2(__FANOTIFY_EVENT_TYPE_NUM - 1) + 1) 257 #define FANOTIFY_EVENT_HASH_BITS \ 258 (32 - FANOTIFY_EVENT_TYPE_BITS) 259 260 struct fanotify_event { 261 struct fsnotify_event fse; 262 struct hlist_node merge_list; /* List for hashed merge */ 263 u32 mask; 264 struct { 265 unsigned int type : FANOTIFY_EVENT_TYPE_BITS; 266 unsigned int hash : FANOTIFY_EVENT_HASH_BITS; 267 }; 268 struct pid *pid; 269 }; 270 271 static inline void fanotify_init_event(struct fanotify_event *event, 272 unsigned int hash, u32 mask) 273 { 274 fsnotify_init_event(&event->fse); 275 INIT_HLIST_NODE(&event->merge_list); 276 event->hash = hash; 277 event->mask = mask; 278 event->pid = NULL; 279 } 280 281 #define FANOTIFY_INLINE_FH(name, size) \ 282 struct { \ 283 struct fanotify_fh name; \ 284 /* Space for filehandle - access with fanotify_fh_buf() */ \ 285 unsigned char _inline_fh_buf[size]; \ 286 } 287 288 struct fanotify_fid_event { 289 struct fanotify_event fae; 290 __kernel_fsid_t fsid; 291 292 FANOTIFY_INLINE_FH(object_fh, FANOTIFY_INLINE_FH_LEN); 293 }; 294 295 static inline struct fanotify_fid_event * 296 FANOTIFY_FE(struct fanotify_event *event) 297 { 298 return container_of(event, struct fanotify_fid_event, fae); 299 } 300 301 struct fanotify_name_event { 302 struct fanotify_event fae; 303 __kernel_fsid_t fsid; 304 struct fanotify_info info; 305 }; 306 307 static inline struct fanotify_name_event * 308 FANOTIFY_NE(struct fanotify_event *event) 309 { 310 return container_of(event, struct fanotify_name_event, fae); 311 } 312 313 struct fanotify_error_event { 314 struct fanotify_event fae; 315 s32 error; /* Error reported by the Filesystem. */ 316 u32 err_count; /* Suppressed errors count */ 317 318 __kernel_fsid_t fsid; /* FSID this error refers to. */ 319 320 FANOTIFY_INLINE_FH(object_fh, MAX_HANDLE_SZ); 321 }; 322 323 static inline struct fanotify_error_event * 324 FANOTIFY_EE(struct fanotify_event *event) 325 { 326 return container_of(event, struct fanotify_error_event, fae); 327 } 328 329 static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event) 330 { 331 if (event->type == FANOTIFY_EVENT_TYPE_FID) 332 return &FANOTIFY_FE(event)->fsid; 333 else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME) 334 return &FANOTIFY_NE(event)->fsid; 335 else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR) 336 return &FANOTIFY_EE(event)->fsid; 337 else 338 return NULL; 339 } 340 341 static inline struct fanotify_fh *fanotify_event_object_fh( 342 struct fanotify_event *event) 343 { 344 if (event->type == FANOTIFY_EVENT_TYPE_FID) 345 return &FANOTIFY_FE(event)->object_fh; 346 else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME) 347 return fanotify_info_file_fh(&FANOTIFY_NE(event)->info); 348 else if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR) 349 return &FANOTIFY_EE(event)->object_fh; 350 else 351 return NULL; 352 } 353 354 static inline struct fanotify_info *fanotify_event_info( 355 struct fanotify_event *event) 356 { 357 if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME) 358 return &FANOTIFY_NE(event)->info; 359 else 360 return NULL; 361 } 362 363 static inline int fanotify_event_object_fh_len(struct fanotify_event *event) 364 { 365 struct fanotify_info *info = fanotify_event_info(event); 366 struct fanotify_fh *fh = fanotify_event_object_fh(event); 367 368 if (info) 369 return info->file_fh_totlen ? fh->len : 0; 370 else 371 return fh ? fh->len : 0; 372 } 373 374 static inline int fanotify_event_dir_fh_len(struct fanotify_event *event) 375 { 376 struct fanotify_info *info = fanotify_event_info(event); 377 378 return info ? fanotify_info_dir_fh_len(info) : 0; 379 } 380 381 static inline int fanotify_event_dir2_fh_len(struct fanotify_event *event) 382 { 383 struct fanotify_info *info = fanotify_event_info(event); 384 385 return info ? fanotify_info_dir2_fh_len(info) : 0; 386 } 387 388 static inline bool fanotify_event_has_object_fh(struct fanotify_event *event) 389 { 390 /* For error events, even zeroed fh are reported. */ 391 if (event->type == FANOTIFY_EVENT_TYPE_FS_ERROR) 392 return true; 393 return fanotify_event_object_fh_len(event) > 0; 394 } 395 396 static inline bool fanotify_event_has_dir_fh(struct fanotify_event *event) 397 { 398 return fanotify_event_dir_fh_len(event) > 0; 399 } 400 401 static inline bool fanotify_event_has_dir2_fh(struct fanotify_event *event) 402 { 403 return fanotify_event_dir2_fh_len(event) > 0; 404 } 405 406 static inline bool fanotify_event_has_any_dir_fh(struct fanotify_event *event) 407 { 408 return fanotify_event_has_dir_fh(event) || 409 fanotify_event_has_dir2_fh(event); 410 } 411 412 struct fanotify_path_event { 413 struct fanotify_event fae; 414 struct path path; 415 }; 416 417 struct fanotify_mnt_event { 418 struct fanotify_event fae; 419 u64 mnt_id; 420 }; 421 422 static inline struct fanotify_path_event * 423 FANOTIFY_PE(struct fanotify_event *event) 424 { 425 return container_of(event, struct fanotify_path_event, fae); 426 } 427 428 static inline struct fanotify_mnt_event * 429 FANOTIFY_ME(struct fanotify_event *event) 430 { 431 return container_of(event, struct fanotify_mnt_event, fae); 432 } 433 434 #define FANOTIFY_NO_RANGE ((loff_t)-1) 435 436 /* 437 * Structure for permission fanotify events. It gets allocated and freed in 438 * fanotify_handle_event() since we wait there for user response. When the 439 * information is retrieved by userspace the structure is moved from 440 * group->notification_list to group->fanotify_data.access_list to wait for 441 * user response. 442 */ 443 struct fanotify_perm_event { 444 struct fanotify_event fae; 445 struct path path; 446 loff_t pos; /* FANOTIFY_NO_RANGE if unavailable */ 447 size_t count; 448 u32 response; /* userspace answer to the event */ 449 unsigned short state; /* state of the event */ 450 unsigned short watchdog_cnt; /* already scanned by watchdog? */ 451 int fd; /* fd we passed to userspace for this event */ 452 pid_t recv_pid; /* pid of task receiving the event */ 453 union { 454 struct fanotify_response_info_header hdr; 455 struct fanotify_response_info_audit_rule audit_rule; 456 }; 457 }; 458 459 static inline struct fanotify_perm_event * 460 FANOTIFY_PERM(struct fanotify_event *event) 461 { 462 return container_of(event, struct fanotify_perm_event, fae); 463 } 464 465 static inline bool fanotify_is_perm_event(u32 mask) 466 { 467 return IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS) && 468 mask & FANOTIFY_PERM_EVENTS; 469 } 470 471 static inline bool fanotify_event_has_access_range(struct fanotify_event *event) 472 { 473 if (!(event->mask & FANOTIFY_PRE_CONTENT_EVENTS)) 474 return false; 475 476 return FANOTIFY_PERM(event)->pos != FANOTIFY_NO_RANGE; 477 } 478 479 static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse) 480 { 481 return container_of(fse, struct fanotify_event, fse); 482 } 483 484 static inline bool fanotify_is_error_event(u32 mask) 485 { 486 return mask & FAN_FS_ERROR; 487 } 488 489 static inline bool fanotify_is_mnt_event(u32 mask) 490 { 491 return mask & (FAN_MNT_ATTACH | FAN_MNT_DETACH); 492 } 493 494 static inline const struct path *fanotify_event_path(struct fanotify_event *event) 495 { 496 if (event->type == FANOTIFY_EVENT_TYPE_PATH) 497 return &FANOTIFY_PE(event)->path; 498 else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM) 499 return &FANOTIFY_PERM(event)->path; 500 else 501 return NULL; 502 } 503 504 /* 505 * Use 128 size hash table to speed up events merge. 506 */ 507 #define FANOTIFY_HTABLE_BITS (7) 508 #define FANOTIFY_HTABLE_SIZE (1 << FANOTIFY_HTABLE_BITS) 509 #define FANOTIFY_HTABLE_MASK (FANOTIFY_HTABLE_SIZE - 1) 510 511 /* 512 * Permission events and overflow event do not get merged - don't hash them. 513 */ 514 static inline bool fanotify_is_hashed_event(u32 mask) 515 { 516 return !(fanotify_is_perm_event(mask) || 517 fsnotify_is_overflow_event(mask)); 518 } 519 520 static inline unsigned int fanotify_event_hash_bucket( 521 struct fsnotify_group *group, 522 struct fanotify_event *event) 523 { 524 return event->hash & FANOTIFY_HTABLE_MASK; 525 } 526 527 struct fanotify_mark { 528 struct fsnotify_mark fsn_mark; 529 __kernel_fsid_t fsid; 530 }; 531 532 static inline struct fanotify_mark *FANOTIFY_MARK(struct fsnotify_mark *mark) 533 { 534 return container_of(mark, struct fanotify_mark, fsn_mark); 535 } 536 537 static inline bool fanotify_fsid_equal(__kernel_fsid_t *fsid1, 538 __kernel_fsid_t *fsid2) 539 { 540 return fsid1->val[0] == fsid2->val[0] && fsid1->val[1] == fsid2->val[1]; 541 } 542 543 static inline unsigned int fanotify_mark_user_flags(struct fsnotify_mark *mark) 544 { 545 unsigned int mflags = 0; 546 547 if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY) 548 mflags |= FAN_MARK_IGNORED_SURV_MODIFY; 549 if (mark->flags & FSNOTIFY_MARK_FLAG_NO_IREF) 550 mflags |= FAN_MARK_EVICTABLE; 551 if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS) 552 mflags |= FAN_MARK_IGNORE; 553 554 return mflags; 555 } 556 557 static inline u32 fanotify_get_response_errno(int res) 558 { 559 return (res >> FAN_ERRNO_SHIFT) & FAN_ERRNO_MASK; 560 } 561