@@ -2196,6 +2196,176 @@ static inline int open_to_namei_flags(int flag)
21962196 return flag ;
21972197}
21982198
2199+ static int may_o_create (struct path * dir , struct dentry * dentry , umode_t mode )
2200+ {
2201+ int error = security_path_mknod (dir , dentry , mode , 0 );
2202+ if (error )
2203+ return error ;
2204+
2205+ error = inode_permission (dir -> dentry -> d_inode , MAY_WRITE | MAY_EXEC );
2206+ if (error )
2207+ return error ;
2208+
2209+ return security_inode_create (dir -> dentry -> d_inode , dentry , mode );
2210+ }
2211+
2212+ static struct file * atomic_open (struct nameidata * nd , struct dentry * dentry ,
2213+ struct path * path , const struct open_flags * op ,
2214+ int * want_write , bool need_lookup ,
2215+ bool * created )
2216+ {
2217+ struct inode * dir = nd -> path .dentry -> d_inode ;
2218+ unsigned open_flag = open_to_namei_flags (op -> open_flag );
2219+ umode_t mode ;
2220+ int error ;
2221+ int acc_mode ;
2222+ struct opendata od ;
2223+ struct file * filp ;
2224+ int create_error = 0 ;
2225+ struct dentry * const DENTRY_NOT_SET = (void * ) -1UL ;
2226+
2227+ BUG_ON (dentry -> d_inode );
2228+
2229+ /* Don't create child dentry for a dead directory. */
2230+ if (unlikely (IS_DEADDIR (dir ))) {
2231+ filp = ERR_PTR (- ENOENT );
2232+ goto out ;
2233+ }
2234+
2235+ mode = op -> mode & S_IALLUGO ;
2236+ if ((open_flag & O_CREAT ) && !IS_POSIXACL (dir ))
2237+ mode &= ~current_umask ();
2238+
2239+ if (open_flag & O_EXCL ) {
2240+ open_flag &= ~O_TRUNC ;
2241+ * created = true;
2242+ }
2243+
2244+ /*
2245+ * Checking write permission is tricky, bacuse we don't know if we are
2246+ * going to actually need it: O_CREAT opens should work as long as the
2247+ * file exists. But checking existence breaks atomicity. The trick is
2248+ * to check access and if not granted clear O_CREAT from the flags.
2249+ *
2250+ * Another problem is returing the "right" error value (e.g. for an
2251+ * O_EXCL open we want to return EEXIST not EROFS).
2252+ */
2253+ if ((open_flag & (O_CREAT | O_TRUNC )) ||
2254+ (open_flag & O_ACCMODE ) != O_RDONLY ) {
2255+ error = mnt_want_write (nd -> path .mnt );
2256+ if (!error ) {
2257+ * want_write = 1 ;
2258+ } else if (!(open_flag & O_CREAT )) {
2259+ /*
2260+ * No O_CREATE -> atomicity not a requirement -> fall
2261+ * back to lookup + open
2262+ */
2263+ goto no_open ;
2264+ } else if (open_flag & (O_EXCL | O_TRUNC )) {
2265+ /* Fall back and fail with the right error */
2266+ create_error = error ;
2267+ goto no_open ;
2268+ } else {
2269+ /* No side effects, safe to clear O_CREAT */
2270+ create_error = error ;
2271+ open_flag &= ~O_CREAT ;
2272+ }
2273+ }
2274+
2275+ if (open_flag & O_CREAT ) {
2276+ error = may_o_create (& nd -> path , dentry , op -> mode );
2277+ if (error ) {
2278+ create_error = error ;
2279+ if (open_flag & O_EXCL )
2280+ goto no_open ;
2281+ open_flag &= ~O_CREAT ;
2282+ }
2283+ }
2284+
2285+ if (nd -> flags & LOOKUP_DIRECTORY )
2286+ open_flag |= O_DIRECTORY ;
2287+
2288+ od .dentry = DENTRY_NOT_SET ;
2289+ od .mnt = nd -> path .mnt ;
2290+ od .filp = & nd -> intent .open .file ;
2291+ filp = dir -> i_op -> atomic_open (dir , dentry , & od , open_flag , mode ,
2292+ created );
2293+ if (IS_ERR (filp )) {
2294+ if (WARN_ON (od .dentry != DENTRY_NOT_SET ))
2295+ dput (od .dentry );
2296+
2297+ if (create_error && PTR_ERR (filp ) == - ENOENT )
2298+ filp = ERR_PTR (create_error );
2299+ goto out ;
2300+ }
2301+
2302+ acc_mode = op -> acc_mode ;
2303+ if (* created ) {
2304+ fsnotify_create (dir , dentry );
2305+ acc_mode = MAY_OPEN ;
2306+ }
2307+
2308+ if (!filp ) {
2309+ if (WARN_ON (od .dentry == DENTRY_NOT_SET )) {
2310+ filp = ERR_PTR (- EIO );
2311+ goto out ;
2312+ }
2313+ if (od .dentry ) {
2314+ dput (dentry );
2315+ dentry = od .dentry ;
2316+ }
2317+ goto looked_up ;
2318+ }
2319+
2320+ /*
2321+ * We didn't have the inode before the open, so check open permission
2322+ * here.
2323+ */
2324+ error = may_open (& filp -> f_path , acc_mode , open_flag );
2325+ if (error )
2326+ goto out_fput ;
2327+
2328+ error = open_check_o_direct (filp );
2329+ if (error )
2330+ goto out_fput ;
2331+
2332+ out :
2333+ dput (dentry );
2334+ return filp ;
2335+
2336+ out_fput :
2337+ fput (filp );
2338+ filp = ERR_PTR (error );
2339+ goto out ;
2340+
2341+ no_open :
2342+ if (need_lookup ) {
2343+ dentry = lookup_real (dir , dentry , nd );
2344+ if (IS_ERR (dentry ))
2345+ return ERR_CAST (dentry );
2346+
2347+ if (create_error ) {
2348+ int open_flag = op -> open_flag ;
2349+
2350+ filp = ERR_PTR (create_error );
2351+ if ((open_flag & O_EXCL )) {
2352+ if (!dentry -> d_inode )
2353+ goto out ;
2354+ } else if (!dentry -> d_inode ) {
2355+ goto out ;
2356+ } else if ((open_flag & O_TRUNC ) &&
2357+ S_ISREG (dentry -> d_inode -> i_mode )) {
2358+ goto out ;
2359+ }
2360+ /* will fail later, go on to get the right error */
2361+ }
2362+ }
2363+ looked_up :
2364+ path -> dentry = dentry ;
2365+ path -> mnt = nd -> path .mnt ;
2366+ return NULL ;
2367+ }
2368+
21992369/*
22002370 * Lookup, maybe create and open the last component
22012371 *
@@ -2219,6 +2389,15 @@ static struct file *lookup_open(struct nameidata *nd, struct path *path,
22192389 if (IS_ERR (dentry ))
22202390 return ERR_CAST (dentry );
22212391
2392+ /* Cached positive dentry: will open in f_op->open */
2393+ if (!need_lookup && dentry -> d_inode )
2394+ goto out_no_open ;
2395+
2396+ if ((nd -> flags & LOOKUP_OPEN ) && dir_inode -> i_op -> atomic_open ) {
2397+ return atomic_open (nd , dentry , path , op , want_write ,
2398+ need_lookup , created );
2399+ }
2400+
22222401 if (need_lookup ) {
22232402 BUG_ON (dentry -> d_inode );
22242403
@@ -2251,6 +2430,7 @@ static struct file *lookup_open(struct nameidata *nd, struct path *path,
22512430 if (error )
22522431 goto out_dput ;
22532432 }
2433+ out_no_open :
22542434 path -> dentry = dentry ;
22552435 path -> mnt = nd -> path .mnt ;
22562436 return NULL ;
@@ -2344,8 +2524,16 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
23442524 filp = lookup_open (nd , path , op , & want_write , & created );
23452525 mutex_unlock (& dir -> d_inode -> i_mutex );
23462526
2347- if (IS_ERR (filp ))
2348- goto out ;
2527+ if (filp ) {
2528+ if (IS_ERR (filp ))
2529+ goto out ;
2530+
2531+ if (created || !S_ISREG (filp -> f_path .dentry -> d_inode -> i_mode ))
2532+ will_truncate = 0 ;
2533+
2534+ audit_inode (pathname , filp -> f_path .dentry );
2535+ goto opened ;
2536+ }
23492537
23502538 if (created ) {
23512539 /* Don't check for write permission, don't truncate */
@@ -2361,6 +2549,16 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
23612549 */
23622550 audit_inode (pathname , path -> dentry );
23632551
2552+ /*
2553+ * If atomic_open() acquired write access it is dropped now due to
2554+ * possible mount and symlink following (this might be optimized away if
2555+ * necessary...)
2556+ */
2557+ if (want_write ) {
2558+ mnt_drop_write (nd -> path .mnt );
2559+ want_write = 0 ;
2560+ }
2561+
23642562 error = - EEXIST ;
23652563 if (open_flag & O_EXCL )
23662564 goto exit_dput ;
@@ -2444,6 +2642,7 @@ static struct file *do_last(struct nameidata *nd, struct path *path,
24442642 retried = true;
24452643 goto retry_lookup ;
24462644 }
2645+ opened :
24472646 if (!IS_ERR (filp )) {
24482647 error = ima_file_check (filp , op -> acc_mode );
24492648 if (error ) {
0 commit comments