Skip to content

Commit 111e540

Browse files
mitagregkh
authored andcommitted
libfs: add DEFINE_SIMPLE_ATTRIBUTE_SIGNED for signed value
[ Upstream commit 2e41f274f9aa71cdcc69dc1f26a3f9304a651804 ] Patch series "fix error when writing negative value to simple attribute files". The simple attribute files do not accept a negative value since the commit 488dac0c9237 ("libfs: fix error cast of negative value in simple_attr_write()"), but some attribute files want to accept a negative value. This patch (of 3): The simple attribute files do not accept a negative value since the commit 488dac0c9237 ("libfs: fix error cast of negative value in simple_attr_write()"), so we have to use a 64-bit value to write a negative value. This adds DEFINE_SIMPLE_ATTRIBUTE_SIGNED for a signed value. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 488dac0c9237 ("libfs: fix error cast of negative value in simple_attr_write()") Signed-off-by: Akinobu Mita <[email protected]> Reported-by: Zhao Gongyi <[email protected]> Reviewed-by: David Hildenbrand <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Jonathan Corbet <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: Rafael J. Wysocki <[email protected]> Cc: Shuah Khan <[email protected]> Cc: Wei Yongjun <[email protected]> Cc: Yicong Yang <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 690a864 commit 111e540

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

fs/libfs.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,8 @@ ssize_t simple_attr_read(struct file *file, char __user *buf,
861861
EXPORT_SYMBOL_GPL(simple_attr_read);
862862

863863
/* interpret the buffer as a number to call the set function with */
864-
ssize_t simple_attr_write(struct file *file, const char __user *buf,
865-
size_t len, loff_t *ppos)
864+
static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf,
865+
size_t len, loff_t *ppos, bool is_signed)
866866
{
867867
struct simple_attr *attr;
868868
unsigned long long val;
@@ -883,7 +883,10 @@ ssize_t simple_attr_write(struct file *file, const char __user *buf,
883883
goto out;
884884

885885
attr->set_buf[size] = '\0';
886-
ret = kstrtoull(attr->set_buf, 0, &val);
886+
if (is_signed)
887+
ret = kstrtoll(attr->set_buf, 0, &val);
888+
else
889+
ret = kstrtoull(attr->set_buf, 0, &val);
887890
if (ret)
888891
goto out;
889892
ret = attr->set(attr->data, val);
@@ -893,8 +896,21 @@ ssize_t simple_attr_write(struct file *file, const char __user *buf,
893896
mutex_unlock(&attr->mutex);
894897
return ret;
895898
}
899+
900+
ssize_t simple_attr_write(struct file *file, const char __user *buf,
901+
size_t len, loff_t *ppos)
902+
{
903+
return simple_attr_write_xsigned(file, buf, len, ppos, false);
904+
}
896905
EXPORT_SYMBOL_GPL(simple_attr_write);
897906

907+
ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
908+
size_t len, loff_t *ppos)
909+
{
910+
return simple_attr_write_xsigned(file, buf, len, ppos, true);
911+
}
912+
EXPORT_SYMBOL_GPL(simple_attr_write_signed);
913+
898914
/**
899915
* generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
900916
* @sb: filesystem to do the file handle conversion on

include/linux/fs.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3132,7 +3132,7 @@ void simple_transaction_set(struct file *file, size_t n);
31323132
* All attributes contain a text representation of a numeric value
31333133
* that are accessed with the get() and set() functions.
31343134
*/
3135-
#define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
3135+
#define DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed) \
31363136
static int __fops ## _open(struct inode *inode, struct file *file) \
31373137
{ \
31383138
__simple_attr_check_format(__fmt, 0ull); \
@@ -3143,10 +3143,16 @@ static const struct file_operations __fops = { \
31433143
.open = __fops ## _open, \
31443144
.release = simple_attr_release, \
31453145
.read = simple_attr_read, \
3146-
.write = simple_attr_write, \
3146+
.write = (__is_signed) ? simple_attr_write_signed : simple_attr_write, \
31473147
.llseek = generic_file_llseek, \
31483148
}
31493149

3150+
#define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt) \
3151+
DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
3152+
3153+
#define DEFINE_SIMPLE_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt) \
3154+
DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
3155+
31503156
static inline __printf(1, 2)
31513157
void __simple_attr_check_format(const char *fmt, ...)
31523158
{
@@ -3161,6 +3167,8 @@ ssize_t simple_attr_read(struct file *file, char __user *buf,
31613167
size_t len, loff_t *ppos);
31623168
ssize_t simple_attr_write(struct file *file, const char __user *buf,
31633169
size_t len, loff_t *ppos);
3170+
ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
3171+
size_t len, loff_t *ppos);
31643172

31653173
struct ctl_table;
31663174
int proc_nr_files(struct ctl_table *table, int write,

0 commit comments

Comments
 (0)