Commit bc15ed66 authored by Mimi Zohar's avatar Mimi Zohar

ima: fix ima_d_path() possible race with rename

On failure to return a pathname from ima_d_path(), a pointer to
dname is returned, which is subsequently used in the IMA measurement
list, the IMA audit records, and other audit logging.  Saving the
pointer to dname for later use has the potential to race with rename.

Intead of returning a pointer to dname on failure, this patch returns
a pointer to a copy of the filename.
Reported-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
Signed-off-by: default avatarMimi Zohar <zohar@linux.vnet.ibm.com>
Cc: stable@vger.kernel.org
parent 710584b9
...@@ -204,7 +204,7 @@ int ima_store_template(struct ima_template_entry *entry, int violation, ...@@ -204,7 +204,7 @@ int ima_store_template(struct ima_template_entry *entry, int violation,
struct inode *inode, struct inode *inode,
const unsigned char *filename, int pcr); const unsigned char *filename, int pcr);
void ima_free_template_entry(struct ima_template_entry *entry); void ima_free_template_entry(struct ima_template_entry *entry);
const char *ima_d_path(const struct path *path, char **pathbuf); const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);
/* IMA policy related functions */ /* IMA policy related functions */
int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask, int ima_match_policy(struct inode *inode, enum ima_hooks func, int mask,
......
...@@ -318,7 +318,17 @@ void ima_audit_measurement(struct integrity_iint_cache *iint, ...@@ -318,7 +318,17 @@ void ima_audit_measurement(struct integrity_iint_cache *iint,
iint->flags |= IMA_AUDITED; iint->flags |= IMA_AUDITED;
} }
const char *ima_d_path(const struct path *path, char **pathbuf) /*
* ima_d_path - return a pointer to the full pathname
*
* Attempt to return a pointer to the full pathname for use in the
* IMA measurement list, IMA audit records, and auditing logs.
*
* On failure, return a pointer to a copy of the filename, not dname.
* Returning a pointer to dname, could result in using the pointer
* after the memory has been freed.
*/
const char *ima_d_path(const struct path *path, char **pathbuf, char *namebuf)
{ {
char *pathname = NULL; char *pathname = NULL;
...@@ -331,5 +341,11 @@ const char *ima_d_path(const struct path *path, char **pathbuf) ...@@ -331,5 +341,11 @@ const char *ima_d_path(const struct path *path, char **pathbuf)
pathname = NULL; pathname = NULL;
} }
} }
return pathname ?: (const char *)path->dentry->d_name.name;
if (!pathname) {
strlcpy(namebuf, path->dentry->d_name.name, NAME_MAX);
pathname = namebuf;
}
return pathname;
} }
...@@ -83,6 +83,7 @@ static void ima_rdwr_violation_check(struct file *file, ...@@ -83,6 +83,7 @@ static void ima_rdwr_violation_check(struct file *file,
const char **pathname) const char **pathname)
{ {
struct inode *inode = file_inode(file); struct inode *inode = file_inode(file);
char filename[NAME_MAX];
fmode_t mode = file->f_mode; fmode_t mode = file->f_mode;
bool send_tomtou = false, send_writers = false; bool send_tomtou = false, send_writers = false;
...@@ -102,7 +103,7 @@ static void ima_rdwr_violation_check(struct file *file, ...@@ -102,7 +103,7 @@ static void ima_rdwr_violation_check(struct file *file,
if (!send_tomtou && !send_writers) if (!send_tomtou && !send_writers)
return; return;
*pathname = ima_d_path(&file->f_path, pathbuf); *pathname = ima_d_path(&file->f_path, pathbuf, filename);
if (send_tomtou) if (send_tomtou)
ima_add_violation(file, *pathname, iint, ima_add_violation(file, *pathname, iint,
...@@ -161,6 +162,7 @@ static int process_measurement(struct file *file, char *buf, loff_t size, ...@@ -161,6 +162,7 @@ static int process_measurement(struct file *file, char *buf, loff_t size,
struct integrity_iint_cache *iint = NULL; struct integrity_iint_cache *iint = NULL;
struct ima_template_desc *template_desc; struct ima_template_desc *template_desc;
char *pathbuf = NULL; char *pathbuf = NULL;
char filename[NAME_MAX];
const char *pathname = NULL; const char *pathname = NULL;
int rc = -ENOMEM, action, must_appraise; int rc = -ENOMEM, action, must_appraise;
int pcr = CONFIG_IMA_MEASURE_PCR_IDX; int pcr = CONFIG_IMA_MEASURE_PCR_IDX;
...@@ -239,8 +241,8 @@ static int process_measurement(struct file *file, char *buf, loff_t size, ...@@ -239,8 +241,8 @@ static int process_measurement(struct file *file, char *buf, loff_t size,
goto out_digsig; goto out_digsig;
} }
if (!pathname) /* ima_rdwr_violation possibly pre-fetched */ if (!pathbuf) /* ima_rdwr_violation possibly pre-fetched */
pathname = ima_d_path(&file->f_path, &pathbuf); pathname = ima_d_path(&file->f_path, &pathbuf, filename);
if (action & IMA_MEASURE) if (action & IMA_MEASURE)
ima_store_measurement(iint, file, pathname, ima_store_measurement(iint, file, pathname,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment