In kernfs_path_from_node_locked(), there is an if statement on line 147 to check whether buf is NULL: if (buf) When buf is NULL, it is used on line 151: len += strlcpy(buf + len, parent_str, ...) and line 158: len += strlcpy(buf + len, "/", ...) and line 160: len += strlcpy(buf + len, kn->name, ...) Thus, possible null-pointer dereferences may occur. To fix these possible bugs, buf is checked before being used. If it is NULL, -EINVAL is returned. These bugs are found by a static analysis tool STCheck written by us.
Unified · Split
| @@ -137,6 +137,9 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to, · 136 unmodified lines | ||
| 137 | 137 | if (kn_from == kn_to) |
| 138 | 138 | return strlcpy(buf, "/", buflen); |
| 139 | 139 | |
| 140 | + if (!buf) | |
| 141 | + return -EINVAL; | |
| 142 | + | |
| 140 | 143 | common = kernfs_common_ancestor(kn_from, kn_to); |
| 141 | 144 | if (WARN_ON(!common)) |
| 142 | 145 | return -EINVAL; |
| @@ -144,8 +147,7 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to, · 1 unmodified line | ||
| 144 | 147 | depth_to = kernfs_depth(common, kn_to); |
| 145 | 148 | depth_from = kernfs_depth(common, kn_from); |
| 146 | 149 | |
| 147 | − if (buf) | |
| 148 | − buf[0] = '\0'; | |
| 150 | + buf[0] = '\0'; | |
| 149 | 151 | |
| 150 | 152 | for (i = 0; i < depth_from; i++) |
| 151 | 153 | len += strlcpy(buf + len, parent_str, |