b41585fc93
-----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEEZH8oZUiU471FcZm+ONu9yGCSaT4FAl8X634ACgkQONu9yGCS aT5sNBAA0JUGRrRlbQMgMtjR7ua+OLi3e07A0QNzD9xHqCc6NEXPuk4yYF8Pcs3g 1ARyEDXybdfUFciChoH0zfBxEbXuB/8+3M3ikE6XV3V/aySzHpfXU/zrepq4kB4G JOqtAxHaLF1tHzsdSZiT3Qj8k8QCcyN9kB08g67JKAe7vd03SKg7CAbBb0JLEUGF 6s7AxohupSJLBWaaQsgYiYMzkH8WceyGMGYydNUNtHLTbHPaj1f5KaRBuHA0Ls57 35ba0suCL3pTbOZ+dF7CqO3mfQgLAY3/vE0jZpoNn3s7BMgOwr3d3cEUhRxI9GPl fO4Ua7Q9NZ67blLwD/fhcua8CeQUXSxqH5YcgDNqQHnZLx7CJW6kEKbFMlqWNaxf yhJbmgZJl+BqTkrIaAbz5FCXO6zAnRehh9L54SzZEdBpp5Y5Ep5nuSkwyZra1KWd b0YK5Yy7dzkZLgPsUc4JrC0o1859IrbvC84fPOk4nuovtCfugnSoIXYjmGQHPFH1 Y5qt/DazsVdzpZIrg2Nvr/LrKh0BOPBjj3SYiCPfZkQDWkKF6ABWSggBxFEqn690 dbJL2A0j+mvuYxUJnkNt0NR3qzXZewHJSqFpCPkN2/TbMNmr9FvKeox9HUIo4qq4 7fqMPs4JNGZHPU7CqxIPJ3s9fjluhkkUtOXZmpzCV/58xI5ZzN0= =3LQn -----END PGP SIGNATURE----- Merge 4.19.134 into android-4.19-stable Changes in 4.19.134 perf: Make perf able to build with latest libbfd net: rmnet: fix lower interface leak genetlink: remove genl_bind ipv4: fill fl4_icmp_{type,code} in ping_v4_sendmsg l2tp: remove skb_dst_set() from l2tp_xmit_skb() llc: make sure applications use ARPHRD_ETHER net: Added pointer check for dst->ops->neigh_lookup in dst_neigh_lookup_skb net_sched: fix a memory leak in atm_tc_init() net: usb: qmi_wwan: add support for Quectel EG95 LTE modem tcp: fix SO_RCVLOWAT possible hangs under high mem pressure tcp: make sure listeners don't initialize congestion-control state tcp: md5: add missing memory barriers in tcp_md5_do_add()/tcp_md5_hash_key() tcp: md5: do not send silly options in SYNCOOKIES tcp: md5: refine tcp_md5_do_add()/tcp_md5_hash_key() barriers tcp: md5: allow changing MD5 keys in all socket states cgroup: fix cgroup_sk_alloc() for sk_clone_lock() cgroup: Fix sock_cgroup_data on big-endian. sched: consistently handle layer3 header accesses in the presence of VLANs vlan: consolidate VLAN parsing code and limit max parsing depth drm/msm: fix potential memleak in error branch drm/exynos: fix ref count leak in mic_pre_enable m68k: nommu: register start of the memory with memblock m68k: mm: fix node memblock init arm64/alternatives: use subsections for replacement sequences tpm_tis: extra chip->ops check on error path in tpm_tis_core_init gfs2: read-only mounts should grab the sd_freeze_gl glock i2c: eg20t: Load module automatically if ID matches arm64/alternatives: don't patch up internal branches iio:magnetometer:ak8974: Fix alignment and data leak issues iio:humidity:hdc100x Fix alignment and data leak issues iio: magnetometer: ak8974: Fix runtime PM imbalance on error iio: mma8452: Add missed iio_device_unregister() call in mma8452_probe() iio: pressure: zpa2326: handle pm_runtime_get_sync failure iio:humidity:hts221 Fix alignment and data leak issues iio:pressure:ms5611 Fix buffer element alignment iio:health:afe4403 Fix timestamp alignment and prevent data leak. spi: fix initial SPI_SR value in spi-fsl-dspi spi: spi-fsl-dspi: Fix lockup if device is shutdown during SPI transfer net: dsa: bcm_sf2: Fix node reference count of: of_mdio: Correct loop scanning logic Revert "usb/ohci-platform: Fix a warning when hibernating" Revert "usb/xhci-plat: Set PM runtime as active on resume" Revert "usb/ehci-platform: Set PM runtime as active on resume" net: sfp: add support for module quirks net: sfp: add some quirks for GPON modules HID: quirks: Remove ITE 8595 entry from hid_have_special_driver ARM: at91: pm: add quirk for sam9x60's ulp1 scsi: sr: remove references to BLK_DEV_SR_VENDOR, leave it enabled ALSA: usb-audio: Create a registration quirk for Kingston HyperX Amp (0951:16d8) doc: dt: bindings: usb: dwc3: Update entries for disabling SS instances in park mode mmc: sdhci: do not enable card detect interrupt for gpio cd type ALSA: usb-audio: Rewrite registration quirk handling ACPI: video: Use native backlight on Acer Aspire 5783z ALSA: usb-audio: Add registration quirk for Kingston HyperX Cloud Alpha S Input: mms114 - add extra compatible for mms345l ACPI: video: Use native backlight on Acer TravelMate 5735Z ALSA: usb-audio: Add registration quirk for Kingston HyperX Cloud Flight S iio:health:afe4404 Fix timestamp alignment and prevent data leak. phy: sun4i-usb: fix dereference of pointer phy0 before it is null checked arm64: dts: meson: add missing gxl rng clock spi: spi-sun6i: sun6i_spi_transfer_one(): fix setting of clock rate usb: gadget: udc: atmel: fix uninitialized read in debug printk staging: comedi: verify array index is correct before using it Revert "thermal: mediatek: fix register index error" ARM: dts: socfpga: Align L2 cache-controller nodename with dtschema regmap: debugfs: Don't sleep while atomic for fast_io regmaps copy_xstate_to_kernel: Fix typo which caused GDB regression apparmor: ensure that dfa state tables have entries perf stat: Zero all the 'ena' and 'run' array slot stats for interval mode soc: qcom: rpmh: Update dirty flag only when data changes soc: qcom: rpmh: Invalidate SLEEP and WAKE TCSes before flushing new data soc: qcom: rpmh-rsc: Clear active mode configuration for wake TCS soc: qcom: rpmh-rsc: Allow using free WAKE TCS for active request mtd: rawnand: marvell: Use nand_cleanup() when the device is not yet registered mtd: rawnand: marvell: Fix probe error path mtd: rawnand: timings: Fix default tR_max and tCCS_min timings mtd: rawnand: brcmnand: fix CS0 layout mtd: rawnand: oxnas: Keep track of registered devices mtd: rawnand: oxnas: Unregister all devices on error mtd: rawnand: oxnas: Release all devices in the _remove() path slimbus: core: Fix mismatch in of_node_get/put HID: magicmouse: do not set up autorepeat HID: quirks: Always poll Obins Anne Pro 2 keyboard HID: quirks: Ignore Simply Automated UPB PIM ALSA: line6: Perform sanity check for each URB creation ALSA: line6: Sync the pending work cancel at disconnection ALSA: usb-audio: Fix race against the error recovery URB submission ALSA: hda/realtek - change to suitable link model for ASUS platform ALSA: hda/realtek - Enable Speaker for ASUS UX533 and UX534 USB: c67x00: fix use after free in c67x00_giveback_urb usb: dwc2: Fix shutdown callback in platform usb: chipidea: core: add wakeup support for extcon usb: gadget: function: fix missing spinlock in f_uac1_legacy USB: serial: iuu_phoenix: fix memory corruption USB: serial: cypress_m8: enable Simply Automated UPB PIM USB: serial: ch341: add new Product ID for CH340 USB: serial: option: add GosunCn GM500 series USB: serial: option: add Quectel EG95 LTE modem virt: vbox: Fix VBGL_IOCTL_VMMDEV_REQUEST_BIG and _LOG req numbers to match upstream virt: vbox: Fix guest capabilities mask check virtio: virtio_console: add missing MODULE_DEVICE_TABLE() for rproc serial serial: mxs-auart: add missed iounmap() in probe failure and remove ovl: inode reference leak in ovl_is_inuse true case. ovl: relax WARN_ON() when decoding lower directory file handle ovl: fix unneeded call to ovl_change_flags() fuse: Fix parameter for FS_IOC_{GET,SET}FLAGS Revert "zram: convert remaining CLASS_ATTR() to CLASS_ATTR_RO()" mei: bus: don't clean driver pointer Input: i8042 - add Lenovo XiaoXin Air 12 to i8042 nomux list uio_pdrv_genirq: fix use without device tree and no interrupt timer: Prevent base->clk from moving backward timer: Fix wheel index calculation on last level MIPS: Fix build for LTS kernel caused by backporting lpj adjustment riscv: use 16KB kernel stack on 64-bit hwmon: (emc2103) fix unable to change fan pwm1_enable attribute powerpc/book3s64/pkeys: Fix pkey_access_permitted() for execute disable pkey intel_th: pci: Add Jasper Lake CPU support intel_th: pci: Add Tiger Lake PCH-H support intel_th: pci: Add Emmitsburg PCH support intel_th: Fix a NULL dereference when hub driver is not loaded dmaengine: fsl-edma: Fix NULL pointer exception in fsl_edma_tx_handler misc: atmel-ssc: lock with mutex instead of spinlock thermal/drivers/cpufreq_cooling: Fix wrong frequency converted from power arm64: ptrace: Override SPSR.SS when single-stepping is enabled arm64: ptrace: Consistently use pseudo-singlestep exceptions arm64: compat: Ensure upper 32 bits of x0 are zero on syscall return sched: Fix unreliable rseq cpu_id for new tasks sched/fair: handle case of task_h_load() returning 0 genirq/affinity: Handle affinity setting on inactive interrupts correctly printk: queue wake_up_klogd irq_work only if per-CPU areas are ready libceph: don't omit recovery_deletes in target_copy() rxrpc: Fix trace string spi: sprd: switch the sequence of setting WDG_LOAD_LOW and _HIGH Linux 4.19.134 Signed-off-by: Greg Kroah-Hartman <gregkh@google.com> Change-Id: Ieeb9e03f4a2d51aeebe3a3eadd9c1b93a26088a0
653 lines
14 KiB
C
653 lines
14 KiB
C
/*
|
|
* Copyright (C) 2017 Red Hat, Inc.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU General Public License version 2 as published by
|
|
* the Free Software Foundation.
|
|
*/
|
|
|
|
#include <linux/cred.h>
|
|
#include <linux/file.h>
|
|
#include <linux/mount.h>
|
|
#include <linux/xattr.h>
|
|
#include <linux/uio.h>
|
|
#include <linux/uaccess.h>
|
|
#include "overlayfs.h"
|
|
|
|
static char ovl_whatisit(struct inode *inode, struct inode *realinode)
|
|
{
|
|
if (realinode != ovl_inode_upper(inode))
|
|
return 'l';
|
|
if (ovl_has_upperdata(inode))
|
|
return 'u';
|
|
else
|
|
return 'm';
|
|
}
|
|
|
|
/* No atime modificaton nor notify on underlying */
|
|
#define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
|
|
|
|
static struct file *ovl_open_realfile(const struct file *file,
|
|
struct inode *realinode)
|
|
{
|
|
struct inode *inode = file_inode(file);
|
|
struct file *realfile;
|
|
const struct cred *old_cred;
|
|
int flags = file->f_flags | OVL_OPEN_FLAGS;
|
|
|
|
old_cred = ovl_override_creds(inode->i_sb);
|
|
realfile = open_with_fake_path(&file->f_path, flags, realinode,
|
|
current_cred());
|
|
ovl_revert_creds(old_cred);
|
|
|
|
pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
|
|
file, file, ovl_whatisit(inode, realinode), file->f_flags,
|
|
realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
|
|
|
|
return realfile;
|
|
}
|
|
|
|
#define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
|
|
|
|
static int ovl_change_flags(struct file *file, unsigned int flags)
|
|
{
|
|
struct inode *inode = file_inode(file);
|
|
int err;
|
|
|
|
flags |= OVL_OPEN_FLAGS;
|
|
|
|
/* If some flag changed that cannot be changed then something's amiss */
|
|
if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
|
|
return -EIO;
|
|
|
|
flags &= OVL_SETFL_MASK;
|
|
|
|
if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
|
|
return -EPERM;
|
|
|
|
if (flags & O_DIRECT) {
|
|
if (!file->f_mapping->a_ops ||
|
|
!file->f_mapping->a_ops->direct_IO)
|
|
return -EINVAL;
|
|
}
|
|
|
|
if (file->f_op->check_flags) {
|
|
err = file->f_op->check_flags(flags);
|
|
if (err)
|
|
return err;
|
|
}
|
|
|
|
spin_lock(&file->f_lock);
|
|
file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
|
|
spin_unlock(&file->f_lock);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
|
|
bool allow_meta)
|
|
{
|
|
struct inode *inode = file_inode(file);
|
|
struct inode *realinode;
|
|
|
|
real->flags = 0;
|
|
real->file = file->private_data;
|
|
|
|
if (allow_meta)
|
|
realinode = ovl_inode_real(inode);
|
|
else
|
|
realinode = ovl_inode_realdata(inode);
|
|
|
|
/* Has it been copied up since we'd opened it? */
|
|
if (unlikely(file_inode(real->file) != realinode)) {
|
|
real->flags = FDPUT_FPUT;
|
|
real->file = ovl_open_realfile(file, realinode);
|
|
|
|
return PTR_ERR_OR_ZERO(real->file);
|
|
}
|
|
|
|
/* Did the flags change since open? */
|
|
if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
|
|
return ovl_change_flags(real->file, file->f_flags);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int ovl_real_fdget(const struct file *file, struct fd *real)
|
|
{
|
|
return ovl_real_fdget_meta(file, real, false);
|
|
}
|
|
|
|
static int ovl_open(struct inode *inode, struct file *file)
|
|
{
|
|
struct file *realfile;
|
|
int err;
|
|
|
|
err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
|
|
if (err)
|
|
return err;
|
|
|
|
/* No longer need these flags, so don't pass them on to underlying fs */
|
|
file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
|
|
|
|
realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
|
|
if (IS_ERR(realfile))
|
|
return PTR_ERR(realfile);
|
|
|
|
file->private_data = realfile;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int ovl_release(struct inode *inode, struct file *file)
|
|
{
|
|
fput(file->private_data);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
|
|
{
|
|
struct inode *inode = file_inode(file);
|
|
struct fd real;
|
|
const struct cred *old_cred;
|
|
loff_t ret;
|
|
|
|
/*
|
|
* The two special cases below do not need to involve real fs,
|
|
* so we can optimizing concurrent callers.
|
|
*/
|
|
if (offset == 0) {
|
|
if (whence == SEEK_CUR)
|
|
return file->f_pos;
|
|
|
|
if (whence == SEEK_SET)
|
|
return vfs_setpos(file, 0, 0);
|
|
}
|
|
|
|
ret = ovl_real_fdget(file, &real);
|
|
if (ret)
|
|
return ret;
|
|
|
|
/*
|
|
* Overlay file f_pos is the master copy that is preserved
|
|
* through copy up and modified on read/write, but only real
|
|
* fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
|
|
* limitations that are more strict than ->s_maxbytes for specific
|
|
* files, so we use the real file to perform seeks.
|
|
*/
|
|
inode_lock(inode);
|
|
real.file->f_pos = file->f_pos;
|
|
|
|
old_cred = ovl_override_creds(inode->i_sb);
|
|
ret = vfs_llseek(real.file, offset, whence);
|
|
ovl_revert_creds(old_cred);
|
|
|
|
file->f_pos = real.file->f_pos;
|
|
inode_unlock(inode);
|
|
|
|
fdput(real);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void ovl_file_accessed(struct file *file)
|
|
{
|
|
struct inode *inode, *upperinode;
|
|
|
|
if (file->f_flags & O_NOATIME)
|
|
return;
|
|
|
|
inode = file_inode(file);
|
|
upperinode = ovl_inode_upper(inode);
|
|
|
|
if (!upperinode)
|
|
return;
|
|
|
|
if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
|
|
!timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
|
|
inode->i_mtime = upperinode->i_mtime;
|
|
inode->i_ctime = upperinode->i_ctime;
|
|
}
|
|
|
|
touch_atime(&file->f_path);
|
|
}
|
|
|
|
static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb)
|
|
{
|
|
int ifl = iocb->ki_flags;
|
|
rwf_t flags = 0;
|
|
|
|
if (ifl & IOCB_NOWAIT)
|
|
flags |= RWF_NOWAIT;
|
|
if (ifl & IOCB_HIPRI)
|
|
flags |= RWF_HIPRI;
|
|
if (ifl & IOCB_DSYNC)
|
|
flags |= RWF_DSYNC;
|
|
if (ifl & IOCB_SYNC)
|
|
flags |= RWF_SYNC;
|
|
|
|
return flags;
|
|
}
|
|
|
|
static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
|
|
{
|
|
struct file *file = iocb->ki_filp;
|
|
struct fd real;
|
|
const struct cred *old_cred;
|
|
ssize_t ret;
|
|
|
|
if (!iov_iter_count(iter))
|
|
return 0;
|
|
|
|
ret = ovl_real_fdget(file, &real);
|
|
if (ret)
|
|
return ret;
|
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
|
ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
|
|
ovl_iocb_to_rwf(iocb));
|
|
ovl_revert_creds(old_cred);
|
|
|
|
ovl_file_accessed(file);
|
|
|
|
fdput(real);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
|
|
{
|
|
struct file *file = iocb->ki_filp;
|
|
struct inode *inode = file_inode(file);
|
|
struct fd real;
|
|
const struct cred *old_cred;
|
|
ssize_t ret;
|
|
|
|
if (!iov_iter_count(iter))
|
|
return 0;
|
|
|
|
inode_lock(inode);
|
|
/* Update mode */
|
|
ovl_copyattr(ovl_inode_real(inode), inode);
|
|
ret = file_remove_privs(file);
|
|
if (ret)
|
|
goto out_unlock;
|
|
|
|
ret = ovl_real_fdget(file, &real);
|
|
if (ret)
|
|
goto out_unlock;
|
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
|
file_start_write(real.file);
|
|
ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
|
|
ovl_iocb_to_rwf(iocb));
|
|
file_end_write(real.file);
|
|
ovl_revert_creds(old_cred);
|
|
|
|
/* Update size */
|
|
ovl_copyattr(ovl_inode_real(inode), inode);
|
|
|
|
fdput(real);
|
|
|
|
out_unlock:
|
|
inode_unlock(inode);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
|
|
{
|
|
struct fd real;
|
|
const struct cred *old_cred;
|
|
int ret;
|
|
|
|
ret = ovl_real_fdget_meta(file, &real, !datasync);
|
|
if (ret)
|
|
return ret;
|
|
|
|
/* Don't sync lower file for fear of receiving EROFS error */
|
|
if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
|
ret = vfs_fsync_range(real.file, start, end, datasync);
|
|
ovl_revert_creds(old_cred);
|
|
}
|
|
|
|
fdput(real);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
|
|
{
|
|
struct file *realfile = file->private_data;
|
|
const struct cred *old_cred;
|
|
int ret;
|
|
|
|
if (!realfile->f_op->mmap)
|
|
return -ENODEV;
|
|
|
|
if (WARN_ON(file != vma->vm_file))
|
|
return -EIO;
|
|
|
|
vma->vm_file = get_file(realfile);
|
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
|
ret = call_mmap(vma->vm_file, vma);
|
|
ovl_revert_creds(old_cred);
|
|
|
|
if (ret) {
|
|
/* Drop reference count from new vm_file value */
|
|
fput(realfile);
|
|
} else {
|
|
/* Drop reference count from previous vm_file value */
|
|
fput(file);
|
|
}
|
|
|
|
ovl_file_accessed(file);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
|
|
{
|
|
struct inode *inode = file_inode(file);
|
|
struct fd real;
|
|
const struct cred *old_cred;
|
|
int ret;
|
|
|
|
ret = ovl_real_fdget(file, &real);
|
|
if (ret)
|
|
return ret;
|
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
|
ret = vfs_fallocate(real.file, mode, offset, len);
|
|
ovl_revert_creds(old_cred);
|
|
|
|
/* Update size */
|
|
ovl_copyattr(ovl_inode_real(inode), inode);
|
|
|
|
fdput(real);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
|
|
{
|
|
struct fd real;
|
|
const struct cred *old_cred;
|
|
int ret;
|
|
|
|
ret = ovl_real_fdget(file, &real);
|
|
if (ret)
|
|
return ret;
|
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
|
ret = vfs_fadvise(real.file, offset, len, advice);
|
|
ovl_revert_creds(old_cred);
|
|
|
|
fdput(real);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static long ovl_real_ioctl(struct file *file, unsigned int cmd,
|
|
unsigned long arg)
|
|
{
|
|
struct fd real;
|
|
const struct cred *old_cred;
|
|
long ret;
|
|
|
|
ret = ovl_real_fdget(file, &real);
|
|
if (ret)
|
|
return ret;
|
|
|
|
old_cred = ovl_override_creds(file_inode(file)->i_sb);
|
|
ret = vfs_ioctl(real.file, cmd, arg);
|
|
ovl_revert_creds(old_cred);
|
|
|
|
fdput(real);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
|
|
unsigned long arg, unsigned int iflags)
|
|
{
|
|
long ret;
|
|
struct inode *inode = file_inode(file);
|
|
unsigned int old_iflags;
|
|
|
|
if (!inode_owner_or_capable(inode))
|
|
return -EACCES;
|
|
|
|
ret = mnt_want_write_file(file);
|
|
if (ret)
|
|
return ret;
|
|
|
|
inode_lock(inode);
|
|
|
|
/* Check the capability before cred override */
|
|
ret = -EPERM;
|
|
old_iflags = READ_ONCE(inode->i_flags);
|
|
if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
|
|
!capable(CAP_LINUX_IMMUTABLE))
|
|
goto unlock;
|
|
|
|
ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
|
|
if (ret)
|
|
goto unlock;
|
|
|
|
ret = ovl_real_ioctl(file, cmd, arg);
|
|
|
|
ovl_copyflags(ovl_inode_real(inode), inode);
|
|
unlock:
|
|
inode_unlock(inode);
|
|
|
|
mnt_drop_write_file(file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
|
|
{
|
|
unsigned int iflags = 0;
|
|
|
|
if (flags & FS_SYNC_FL)
|
|
iflags |= S_SYNC;
|
|
if (flags & FS_APPEND_FL)
|
|
iflags |= S_APPEND;
|
|
if (flags & FS_IMMUTABLE_FL)
|
|
iflags |= S_IMMUTABLE;
|
|
if (flags & FS_NOATIME_FL)
|
|
iflags |= S_NOATIME;
|
|
|
|
return iflags;
|
|
}
|
|
|
|
static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
|
|
unsigned long arg)
|
|
{
|
|
unsigned int flags;
|
|
|
|
if (get_user(flags, (int __user *) arg))
|
|
return -EFAULT;
|
|
|
|
return ovl_ioctl_set_flags(file, cmd, arg,
|
|
ovl_fsflags_to_iflags(flags));
|
|
}
|
|
|
|
static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
|
|
{
|
|
unsigned int iflags = 0;
|
|
|
|
if (xflags & FS_XFLAG_SYNC)
|
|
iflags |= S_SYNC;
|
|
if (xflags & FS_XFLAG_APPEND)
|
|
iflags |= S_APPEND;
|
|
if (xflags & FS_XFLAG_IMMUTABLE)
|
|
iflags |= S_IMMUTABLE;
|
|
if (xflags & FS_XFLAG_NOATIME)
|
|
iflags |= S_NOATIME;
|
|
|
|
return iflags;
|
|
}
|
|
|
|
static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
|
|
unsigned long arg)
|
|
{
|
|
struct fsxattr fa;
|
|
|
|
memset(&fa, 0, sizeof(fa));
|
|
if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
|
|
return -EFAULT;
|
|
|
|
return ovl_ioctl_set_flags(file, cmd, arg,
|
|
ovl_fsxflags_to_iflags(fa.fsx_xflags));
|
|
}
|
|
|
|
static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
|
{
|
|
long ret;
|
|
|
|
switch (cmd) {
|
|
case FS_IOC_GETFLAGS:
|
|
case FS_IOC_FSGETXATTR:
|
|
ret = ovl_real_ioctl(file, cmd, arg);
|
|
break;
|
|
|
|
case FS_IOC_SETFLAGS:
|
|
ret = ovl_ioctl_set_fsflags(file, cmd, arg);
|
|
break;
|
|
|
|
case FS_IOC_FSSETXATTR:
|
|
ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
|
|
break;
|
|
|
|
default:
|
|
ret = -ENOTTY;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
|
|
unsigned long arg)
|
|
{
|
|
switch (cmd) {
|
|
case FS_IOC32_GETFLAGS:
|
|
cmd = FS_IOC_GETFLAGS;
|
|
break;
|
|
|
|
case FS_IOC32_SETFLAGS:
|
|
cmd = FS_IOC_SETFLAGS;
|
|
break;
|
|
|
|
default:
|
|
return -ENOIOCTLCMD;
|
|
}
|
|
|
|
return ovl_ioctl(file, cmd, arg);
|
|
}
|
|
|
|
enum ovl_copyop {
|
|
OVL_COPY,
|
|
OVL_CLONE,
|
|
OVL_DEDUPE,
|
|
};
|
|
|
|
static ssize_t ovl_copyfile(struct file *file_in, loff_t pos_in,
|
|
struct file *file_out, loff_t pos_out,
|
|
u64 len, unsigned int flags, enum ovl_copyop op)
|
|
{
|
|
struct inode *inode_out = file_inode(file_out);
|
|
struct fd real_in, real_out;
|
|
const struct cred *old_cred;
|
|
ssize_t ret;
|
|
|
|
ret = ovl_real_fdget(file_out, &real_out);
|
|
if (ret)
|
|
return ret;
|
|
|
|
ret = ovl_real_fdget(file_in, &real_in);
|
|
if (ret) {
|
|
fdput(real_out);
|
|
return ret;
|
|
}
|
|
|
|
old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
|
|
switch (op) {
|
|
case OVL_COPY:
|
|
ret = vfs_copy_file_range(real_in.file, pos_in,
|
|
real_out.file, pos_out, len, flags);
|
|
break;
|
|
|
|
case OVL_CLONE:
|
|
ret = vfs_clone_file_range(real_in.file, pos_in,
|
|
real_out.file, pos_out, len);
|
|
break;
|
|
|
|
case OVL_DEDUPE:
|
|
ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
|
|
real_out.file, pos_out, len);
|
|
break;
|
|
}
|
|
ovl_revert_creds(old_cred);
|
|
|
|
/* Update size */
|
|
ovl_copyattr(ovl_inode_real(inode_out), inode_out);
|
|
|
|
fdput(real_in);
|
|
fdput(real_out);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
|
|
struct file *file_out, loff_t pos_out,
|
|
size_t len, unsigned int flags)
|
|
{
|
|
return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
|
|
OVL_COPY);
|
|
}
|
|
|
|
static int ovl_clone_file_range(struct file *file_in, loff_t pos_in,
|
|
struct file *file_out, loff_t pos_out, u64 len)
|
|
{
|
|
return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, 0,
|
|
OVL_CLONE);
|
|
}
|
|
|
|
static int ovl_dedupe_file_range(struct file *file_in, loff_t pos_in,
|
|
struct file *file_out, loff_t pos_out, u64 len)
|
|
{
|
|
/*
|
|
* Don't copy up because of a dedupe request, this wouldn't make sense
|
|
* most of the time (data would be duplicated instead of deduplicated).
|
|
*/
|
|
if (!ovl_inode_upper(file_inode(file_in)) ||
|
|
!ovl_inode_upper(file_inode(file_out)))
|
|
return -EPERM;
|
|
|
|
return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, 0,
|
|
OVL_DEDUPE);
|
|
}
|
|
|
|
const struct file_operations ovl_file_operations = {
|
|
.open = ovl_open,
|
|
.release = ovl_release,
|
|
.llseek = ovl_llseek,
|
|
.read_iter = ovl_read_iter,
|
|
.write_iter = ovl_write_iter,
|
|
.fsync = ovl_fsync,
|
|
.mmap = ovl_mmap,
|
|
.fallocate = ovl_fallocate,
|
|
.fadvise = ovl_fadvise,
|
|
.unlocked_ioctl = ovl_ioctl,
|
|
.compat_ioctl = ovl_compat_ioctl,
|
|
|
|
.copy_file_range = ovl_copy_file_range,
|
|
.clone_file_range = ovl_clone_file_range,
|
|
.dedupe_file_range = ovl_dedupe_file_range,
|
|
};
|