Whamcloud - gitweb
LU-4563 Fix unsafe userspace access in many proc files
[fs/lustre-release.git] / lustre / mdt / mdt_lproc.c
index 6953802..2603c79 100644 (file)
@@ -229,13 +229,13 @@ int mdt_procfs_init(struct mdt_device *mdt, const char *name)
                       mdt_obd_name(mdt), rc);
                return rc;
        }
+
        rc = hsm_cdt_procfs_init(mdt);
        if (rc) {
                CERROR("%s: cannot create hsm proc entries: rc = %d\n",
                       mdt_obd_name(mdt), rc);
                return rc;
        }
-       ptlrpc_lprocfs_register_obd(obd);
 
        obd->obd_proc_exports_entry = proc_mkdir("exports",
                                                 obd->obd_proc_entry);
@@ -263,14 +263,14 @@ void mdt_procfs_fini(struct mdt_device *mdt)
 {
        struct obd_device *obd = mdt2obd_dev(mdt);
 
-       if (obd->obd_proc_exports_entry) {
+       if (obd->obd_proc_exports_entry != NULL) {
                lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
                obd->obd_proc_exports_entry = NULL;
        }
+
        lprocfs_free_per_client_stats(obd);
-       lprocfs_obd_cleanup(obd);
        hsm_cdt_procfs_fini(mdt);
-       ptlrpc_lprocfs_unregister_obd(obd);
+       lprocfs_obd_cleanup(obd);
        lprocfs_free_md_stats(obd);
        lprocfs_free_obd_stats(obd);
        lprocfs_job_stats_fini(obd);
@@ -287,8 +287,9 @@ static int lprocfs_rd_identity_expire(char *page, char **start, off_t off,
                         mdt->mdt_identity_cache->uc_entry_expire);
 }
 
-static int lprocfs_wr_identity_expire(struct file *file, const char *buffer,
-                                      unsigned long count, void *data)
+static int lprocfs_wr_identity_expire(struct file *file,
+                                     const char __user *buffer,
+                                     unsigned long count, void *data)
 {
         struct obd_device *obd = data;
         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
@@ -315,9 +316,9 @@ static int lprocfs_rd_identity_acquire_expire(char *page, char **start,
 }
 
 static int lprocfs_wr_identity_acquire_expire(struct file *file,
-                                              const char *buffer,
-                                              unsigned long count,
-                                              void *data)
+                                             const char __user *buffer,
+                                             unsigned long count,
+                                             void *data)
 {
         struct obd_device *obd = data;
         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
@@ -346,7 +347,8 @@ static int lprocfs_rd_identity_upcall(char *page, char **start, off_t off,
        return len;
 }
 
-static int lprocfs_wr_identity_upcall(struct file *file, const char *buffer,
+static int lprocfs_wr_identity_upcall(struct file *file,
+                                     const char __user *buffer,
                                      unsigned long count, void *data)
 {
        struct obd_device       *obd = data;
@@ -389,8 +391,9 @@ static int lprocfs_wr_identity_upcall(struct file *file, const char *buffer,
        RETURN(rc);
 }
 
-static int lprocfs_wr_identity_flush(struct file *file, const char *buffer,
-                                     unsigned long count, void *data)
+static int lprocfs_wr_identity_flush(struct file *file,
+                                    const char __user *buffer,
+                                    unsigned long count, void *data)
 {
         struct obd_device *obd = data;
         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
@@ -404,7 +407,8 @@ static int lprocfs_wr_identity_flush(struct file *file, const char *buffer,
         return count;
 }
 
-static int lprocfs_wr_identity_info(struct file *file, const char *buffer,
+static int lprocfs_wr_identity_info(struct file *file,
+                                   const char __user *buffer,
                                    unsigned long count, void *data)
 {
        struct obd_device *obd = data;
@@ -473,15 +477,15 @@ out:
 static int lprocfs_rd_capa(char *page, char **start, off_t off,
                            int count, int *eof, void *data)
 {
-        struct obd_device *obd = data;
-        struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
+       struct obd_device *obd = data;
+       struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
 
-        return snprintf(page, count, "capability on: %s %s\n",
-                        mdt->mdt_opts.mo_oss_capa ? "oss" : "",
-                        mdt->mdt_opts.mo_mds_capa ? "mds" : "");
+       return snprintf(page, count, "capability on: %s %s\n",
+                       mdt->mdt_lut.lut_oss_capa ? "oss" : "",
+                       mdt->mdt_lut.lut_mds_capa ? "mds" : "");
 }
 
-static int lprocfs_wr_capa(struct file *file, const char *buffer,
+static int lprocfs_wr_capa(struct file *file, const char __user *buffer,
                           unsigned long count, void *data)
 {
        struct obd_device *obd = data;
@@ -508,15 +512,17 @@ static int lprocfs_wr_capa(struct file *file, const char *buffer,
                return -EINVAL;
        }
 
-       mdt->mdt_opts.mo_oss_capa = (val & 0x1);
-       mdt->mdt_opts.mo_mds_capa = !!(val & 0x2);
+       spin_lock(&mdt->mdt_lut.lut_flags_lock);
+       mdt->mdt_lut.lut_oss_capa = !!(val & 0x1);
+       mdt->mdt_lut.lut_mds_capa = !!(val & 0x2);
+       spin_unlock(&mdt->mdt_lut.lut_flags_lock);
        mdt->mdt_capa_conf = 1;
        LCONSOLE_INFO("MDS %s %s MDS fid capability.\n",
                      mdt_obd_name(mdt),
-                     mdt->mdt_opts.mo_mds_capa ? "enabled" : "disabled");
+                     mdt->mdt_lut.lut_mds_capa ? "enabled" : "disabled");
        LCONSOLE_INFO("MDS %s %s OSS fid capability.\n",
                      mdt_obd_name(mdt),
-                     mdt->mdt_opts.mo_oss_capa ? "enabled" : "disabled");
+                     mdt->mdt_lut.lut_oss_capa ? "enabled" : "disabled");
        return count;
 }
 
@@ -546,8 +552,8 @@ static int lprocfs_rd_capa_timeout(char *page, char **start, off_t off,
         return snprintf(page, count, "%lu\n", mdt->mdt_capa_timeout);
 }
 
-static int lprocfs_wr_capa_timeout(struct file *file, const char *buffer,
-                                   unsigned long count, void *data)
+static int lprocfs_wr_capa_timeout(struct file *file, const char __user *buffer,
+                                  unsigned long count, void *data)
 {
         struct obd_device *obd = data;
         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
@@ -571,8 +577,8 @@ static int lprocfs_rd_ck_timeout(char *page, char **start, off_t off, int count,
         return snprintf(page, count, "%lu\n", mdt->mdt_ck_timeout);
 }
 
-static int lprocfs_wr_ck_timeout(struct file *file, const char *buffer,
-                                 unsigned long count, void *data)
+static int lprocfs_wr_ck_timeout(struct file *file, const char __user *buffer,
+                                unsigned long count, void *data)
 {
         struct obd_device *obd = data;
         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
@@ -589,8 +595,9 @@ static int lprocfs_wr_ck_timeout(struct file *file, const char *buffer,
 
 #define BUFLEN (UUID_MAX + 4)
 
-static int lprocfs_mdt_wr_evict_client(struct file *file, const char *buffer,
-                                       unsigned long count, void *data)
+static int lprocfs_mdt_wr_evict_client(struct file *file,
+                                      const char __user *buffer,
+                                      unsigned long count, void *data)
 {
         char *kbuf;
         char *tmpbuf;
@@ -631,11 +638,11 @@ static int lprocfs_rd_sec_level(char *page, char **start, off_t off,
         struct obd_device *obd = data;
         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
 
-        return snprintf(page, count, "%d\n", mdt->mdt_sec_level);
+       return snprintf(page, count, "%d\n", mdt->mdt_lut.lut_sec_level);
 }
 
-static int lprocfs_wr_sec_level(struct file *file, const char *buffer,
-                                unsigned long count, void *data)
+static int lprocfs_wr_sec_level(struct file *file, const char __user *buffer,
+                               unsigned long count, void *data)
 {
         struct obd_device *obd = data;
         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
@@ -654,8 +661,8 @@ static int lprocfs_wr_sec_level(struct file *file, const char *buffer,
                 return -EINVAL;
         }
 
-        mdt->mdt_sec_level = val;
-        return count;
+       mdt->mdt_lut.lut_sec_level = val;
+       return count;
 }
 
 static int lprocfs_rd_cos(char *page, char **start, off_t off,
@@ -667,8 +674,8 @@ static int lprocfs_rd_cos(char *page, char **start, off_t off,
         return snprintf(page, count, "%u\n", mdt_cos_is_enabled(mdt));
 }
 
-static int lprocfs_wr_cos(struct file *file, const char *buffer,
-                                  unsigned long count, void *data)
+static int lprocfs_wr_cos(struct file *file, const char __user *buffer,
+                         unsigned long count, void *data)
 {
         struct obd_device *obd = data;
         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
@@ -706,29 +713,29 @@ static int safe_strtoul(const char *str, char **endp, unsigned long *res)
         return 0;
 }
 
-static int lprocfs_wr_root_squash(struct file *file, const char *buffer,
+static int lprocfs_wr_root_squash(struct file *file, const char __user *buffer,
                                  unsigned long count, void *data)
 {
        struct obd_device *obd = data;
        struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
        int rc;
-       char kernbuf[50], *tmp, *end, *errmsg;
+       char kernbuf[64], *tmp, *end, *errmsg;
        unsigned long uid, gid;
        int nouid, nogid;
        ENTRY;
 
        if (count >= sizeof(kernbuf)) {
                errmsg = "string too long";
-               GOTO(failed, rc = -EINVAL);
+               GOTO(failed_noprint, rc = -EINVAL);
        }
        if (copy_from_user(kernbuf, buffer, count)) {
                errmsg = "bad address";
-               GOTO(failed, rc = -EFAULT);
+               GOTO(failed_noprint, rc = -EFAULT);
        }
        kernbuf[count] = '\0';
 
        nouid = nogid = 0;
-       if (safe_strtoul(buffer, &tmp, &uid)) {
+       if (safe_strtoul(kernbuf, &tmp, &uid)) {
                uid = mdt->mdt_squash_uid;
                nouid = 1;
        }
@@ -760,7 +767,11 @@ static int lprocfs_wr_root_squash(struct file *file, const char *buffer,
 
 failed:
        CWARN("%s: failed to set root_squash to \"%s\", %s: rc %d\n",
-             mdt_obd_name(mdt), buffer, errmsg, rc);
+             mdt_obd_name(mdt), kernbuf, errmsg, rc);
+       RETURN(rc);
+failed_noprint:
+       CWARN("%s: failed to set root_squash due to %s: rc %d\n",
+             mdt_obd_name(mdt), errmsg, rc);
        RETURN(rc);
 }
 
@@ -775,7 +786,8 @@ static int lprocfs_rd_nosquash_nids(char *page, char **start, off_t off,
         return snprintf(page, count, "NONE\n");
 }
 
-static int lprocfs_wr_nosquash_nids(struct file *file, const char *buffer,
+static int lprocfs_wr_nosquash_nids(struct file *file,
+                                   const char __user *buffer,
                                    unsigned long count, void *data)
 {
        struct obd_device *obd = data;
@@ -851,8 +863,8 @@ static int lprocfs_rd_mdt_som(char *page, char **start, off_t off,
                         mdt->mdt_som_conf ? "en" : "dis");
 }
 
-static int lprocfs_wr_mdt_som(struct file *file, const char *buffer,
-                              unsigned long count, void *data)
+static int lprocfs_wr_mdt_som(struct file *file, const char __user *buffer,
+                             unsigned long count, void *data)
 {
         struct obd_export *exp;
         struct obd_device *obd = data;
@@ -910,7 +922,8 @@ static int lprocfs_rd_enable_remote_dir(char *page, char **start, off_t off,
        return snprintf(page, count, "%u\n", mdt->mdt_enable_remote_dir);
 }
 
-static int lprocfs_wr_enable_remote_dir(struct file *file, const char *buffer,
+static int lprocfs_wr_enable_remote_dir(struct file *file,
+                                       const char __user *buffer,
                                        unsigned long count, void *data)
 {
        struct obd_device *obd = data;
@@ -940,7 +953,7 @@ static int lprocfs_rd_enable_remote_dir_gid(char *page, char **start, off_t off,
 }
 
 static int lprocfs_wr_enable_remote_dir_gid(struct file *file,
-                                           const char *buffer,
+                                           const char __user *buffer,
                                            unsigned long count, void *data)
 {
        struct obd_device *obd = data;
@@ -1049,6 +1062,59 @@ struct lprocfs_vars lprocfs_mds_module_vars[] = {
        { 0 }
 };
 
+int lprocfs_mdt_print_open_files(cfs_hash_t *hs, cfs_hash_bd_t *bd,
+                                cfs_hlist_node_t *hnode, void *v)
+{
+       struct obd_export       *exp = cfs_hash_object(hs, hnode);
+       struct seq_file         *seq = v;
+
+       if (exp->exp_lock_hash != NULL) {
+               struct mdt_export_data  *med = &exp->exp_mdt_data;
+               struct mdt_file_data    *mfd;
+
+               spin_lock(&med->med_open_lock);
+               cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
+                       seq_printf(seq, DFID"\n",
+                                  PFID(mdt_object_fid(mfd->mfd_object)));
+               }
+               spin_unlock(&med->med_open_lock);
+       }
+
+       return 0;
+}
+
+int lprocfs_mdt_open_files_seq_show(struct seq_file *seq, void *v)
+{
+       struct nid_stat *stats = seq->private;
+       struct obd_device *obd = stats->nid_obd;
+
+       cfs_hash_for_each_key(obd->obd_nid_hash, &stats->nid,
+                             lprocfs_mdt_print_open_files, seq);
+
+       return 0;
+}
+
+int lprocfs_mdt_open_files_seq_open(struct inode *inode, struct file *file)
+{
+       struct proc_dir_entry   *dp = PDE(inode);
+       struct seq_file         *seq;
+       struct nid_stat         *tmp;
+       int                     rc;
+
+       if (LPROCFS_ENTRY_CHECK(dp))
+               return -ENOENT;
+
+       tmp = dp->data;
+       rc = single_open(file, &lprocfs_mdt_open_files_seq_show, NULL);
+       if (rc != 0)
+               return rc;
+
+       seq = file->private_data;
+       seq->private = tmp;
+
+       return 0;
+}
+
 void mdt_counter_incr(struct ptlrpc_request *req, int opcode)
 {
        struct obd_export *exp = req->rq_export;